变量声明及其作用域局部变量VARNAME=VALUE:作用域是整个bash进程【新开一个shell进程,没有这个变量】[root@jiakanga]#A=123[root@jiakanga]#bash[root@jiakanga]#echo$A[root@jiakanga]#局部变量localVARNAME=VALUE:作用域为当前代码段环境变量作用域为当前shell及其子进程【打开一个shell就是一个进程】exportVARNAME=VALUEorVARNAME=VALUEexportVARNAME[root@jiakanga]#exportNAME=jiakang[root@jiakanga]#bash[root@jiakanga]#echo$NAMEjiakangpositionvariable$1,$2,...[参数1,parameter2...]shift:去掉变量[root@jiakanga]#cattestshift.sh#!/bin/bashecho$1shiftecho"去掉第一个参数,现在$1是原来的第二个参数"echo$1shiftecho"的第三个,以此类推,一个一个去掉"echo$1[root@jiakanga]#./testshift.sh1231第一个参数去掉,现在2是原来的第二个参数2三,以此类推,去掉3个特殊变量一个一个$?:上一条命令执行状态的返回值$#:参数个数$*:参数列表$@:参数列表[root@jiakanga]#cattestvar.sh#!/bin/bashecho$#echo$*echo$@[root@jiakanga]#./testvar.sh0[root@jiakanga]#./testvar.shtestif.shtest.sh2testif.sh测试。shtestif.shtest.shunsetthevariableunsetVARNAME查看当前shell变量set查看当前shell环境变量env,printenv,exportscript脚本执行有两种方式,一种是直接调用shell执行,使用作为bash参数的脚本代码[root@jiakanga]#cattest.shls-l[root@jiakanga]#bashtest.sh总使用量8drwxr-xr-x。3rootroot40963月28日16:34b-rw-r--r--。1rootroot7April420:13test.sh二是开头声明magicnumber,脚本写好后加上执行权限magicnumber:shebang[脚本第一行,调用哪个shell执行脚本]#!/bin/bash[root@jiakanga]#vitest.sh[root@jiakanga]#cattest.sh#!/bin/bashls-lecho"secondmiddle,magicnumber"[root@jiakanga]#chmod+xtest.sh[root@jiakanga]#./test.sh总共使用8drwxr-xr-x。3rootroot4096March2816:34b修改环境变量$PATH使用:VALUE可以组装:VALUE和变量,使用这个方法可以修改环境变量[root@jiakanga]#A=root[root@jiakanga]#A=$A:usr[root@jiakanga]#echo$Aroot:usr[root@jiakanga]#exportPATH=$PATH:/bin[root@jiakanga]#echo$PATH/usr/lib/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/bin/bash:/bin算术bash中不能进行运算,可以使用以下方法进行算术运算A=3B=61,let算术运算表达式letC=$A+$B2,$[算术表达式]C=[$A+$B]3,$((算术表达式))C=$(($A+$B))4,expr`算术表达式`[操作数和运算符之间必须有空格,并使用命令引用]C=`expr$A+$B`
