在bash中${}用于设置变量默认值和字符串取值切片以及变量的间接引用,详细用法如下图,在Centos6下字符串取子${STR:POSITON:LENGTH},LENGTH为负数会报错。
1、${VAR},取出变量VAR值
[root@localhost~]#a=hjks [root@localhost~]#echo${a} hjks
2、${VAR:-DEFAULT},VAR没有定义或者为空则输出$DEFAULT的值(VAR不变)
[root@localhost~]#unseta [root@localhost~]#echo${a:-hello};echo$a hello #$a未赋值输出了空值 #可以再脚本用于设置默认值
3、${VAR:=DEFAULT},VAR没有定义或空则为$DEFAULT的值
[root@localhost~]#echo${a:=hello};echoa hello hello#a被赋值
4、${VAR+VALUE},${VAR+VALUE},VAR定义则输出$VALUE,否则为空字符串(VAR不变)
[root@localhost~]#a=hello [root@localhost~]#echo${a:+yemo};echo$a yemo hello [root@localhost~]#unseta [root@localhost~]#echo${a:+yemo};echo$a
5、${VAR?MSG},VAR未定义则打印$MSG
[root@localhost~]#unseta [root@localhost~]#echo${a?thisisnull} -bash:a:thisisnull [root@localhost~]#typeset-ia [root@localhost~]#echo${a?thisisnull} -bash:a:thisisnull
6、${VAR:?MSG},VAR未定义或者为空则打印$MSG
[root@localhost~]#unseta [root@localhost~]#echo${a?thisisnull} -bash:a:thisisnull [root@localhost~]#typeset-ia [root@localhost~]#echo${a?thisisnull} -bash:a:thisisnull
7、${!PERFIX*} ${!PERFIX@}匹配所有以PERFIX开头的变量
[root@localhost~]#echo${!RAND*} RANDOM [root@localhost~]#echo${!RAND@} RANDOM
8、${!VAR}变量间接引用,也可以用eval echo \\$$VAR
[root@localhost~]#a=b [root@localhost~]#b=12345 [root@localhost~]#echo${!a} 12345 [root@localhost~]#evalecho\$$a 12345
9、${#STR},返回STR长度
[root@localhost~]#b=12345 [root@localhost~]#echo${#b} 5
10、${STR^^},把STR中的所有小写字母转换为大写输出(不改变值)
[root@localhost~]#unsetab [root@localhost~]#a="helloRieKugimiya"#钉宫理惠老师,声音很萌很可爱 [root@localhost~]#echo${a^^} HELLORIEKUGIMIYA [root@localhost~]#echo$a helloRieKugimiya
11、${STR,},把STR中的所有大写字母转换为小写输出(不改变值)
[root@localhost~]#echo${a,} helloriekugimiya [root@localhost~]#echo$a helloRieKugimiya
12、${STR:POSITON},从$POSITON位置提取子串
[root@localhost~]#echo$a helloRieKugimiya [root@localhost~]#echo${a:5} RieKugimiya#正向取子串 [root@localhost~]#echo${a:-5}#反向从末尾开始计数$POSITON位置的子串 imiya
13、${STR:POSITON:LENGTH},从$POSITON位置提取长度为$LENGTH子串
CentOS_7 [root@localhost~]#echo$a helloRieKugimiya [root@localhost~]#echo${a:5:6}#从第5个开始向后取6个字符 RieK [root@localhost~]#echo${a:5:-3}#从第五个开始去去掉倒数3个字符 RieKugim [root@localhost~]#echo${a:-5:-3}#取倒数5个字符去掉倒数3个字符 im#由于减号是前面的关键字所以POSITON前面得加空格 CentOS_6 [root@localhost~]#a="daisukiRieKugimiya"#依旧是可爱的钉宫理惠老师 [root@localhost~]#echo${a:5:6} kiRie [root@localhost~]#echo${a:5:-4} -bash:-4:substringexpression<0 [root@localhost~]#echo${a:-5:-2} -bash:-2:substringexpression<0 [root@localhost~]#echo${a:-5:2}#从-5位置开始去2个字符长度的子串 im
14、${STR#SUBSTR},从$STR头查找匹配,删除最短匹配$SUBSTR的子串
${arry#SUBSTR},对于数组,对每个数组元素单独匹配,执行删除子串操作
[root@localhost~]#echo${a} daisukiRieKugimiya [root@localhost~]#echo${a#*R} ieKugimiya#从左向右匹配*R匹配到daisukiR,删除并返回剩下的 #数组 [root@localhost~]#b=(daisukiRieKugimiya) [root@localhost~]#echo${b[@]} daisukiRieKugimiya [root@localhost~]#echo${b[@]#*i}#删除了b[0]的dai,b[1]的Ri,b[2]的Kugi sukiemiya
15、${STR##SUBSTR},从$STR头部查找匹配,删除最长匹配$SUBSTR的子串
[root@localhost~]#echo$a#前面很详细了,后面就不解释了 daisukiRieKugimiya [root@localhost~]#echo${a##*i} ya [root@localhost~]#echo${b[@]##*i} eya
16、${STR%SUBSTR},从$STR尾部查找匹配,删除最短匹配$SUBSTR的子串
[root@localhost~]#echo${a%i*} daisukiRieKugim [root@localhost~]#echo${b[@]%i*} daisukRKugim
17、${STR%%SUBSTR},从$STR尾部查找匹配,删除最长匹配$SUBSTR的子串
[root@localhost~]#echo${a%%i*} da [root@localhost~]#echo${b[@]%%i*} daRKug
18、${STR/SUBSTR/REPLACE},使用$REPALCE替换$STR第一个匹配的$SUBSTR
[root@localhost~]#echo${a/i/%%/} da%%/sukiRieKugimiya [root@localhost~]#echo${b[@]/i/%%/} da%%/sukiR%%/eKug%%/miya
19、${STR//SUBSTR/REPLACE},使用$REPALCE替换$STR中所有匹配的$SUBSTR
[root@localhost~]#echo${a//i/%%/} da%%/suk%%/R%%/eKug%%/m%%/ya [root@localhost~]#echo${b[@]//i/%%/} da%%/suk%%/R%%/eKug%%/m%%/ya
20、${STR/#SUBSTR/REPLACE},$STR以$STR开头的,则用$REPLACE来替换匹配到的$SUBSTR
[root@localhost~]#echo${a/#su/%%} daisukiRieKugimiya [root@localhost~]#echo${a/#dai/%%} %%sukiRieKugimiya [root@localhost~]#echo${b[@]/#K/@} daisukiRie@ugimiya [root@localhost~]#echo${b[@]/#K/$$} daisukiRie1723ugimiya [root@localhost~]#echo${b[@]/#K/$#} daisukiRie0ugimiya [root@localhost~]#echo${b[@]/#K/666} daisukiRie666ugimiya
21、${STR/%SUBSTR/REPLACE},$STR以$STR结尾的,则用$REPLACE来替换匹配到的$SUBSTR
[root@localhost~]#echo${a/%dai/##} daisukiRieKugimiya [root@localhost~]#echo${a/%miya/##} daisukiRieKugi## [root@localhost~]#echo${b[@]/%ie/666} daisukiR666Kugimiya [root@localhost~]#echo${b[@]/%ie/$$} daisukiR1723Kugimiya
Bash函数或者脚本接受连续参数到数组
#1、使用shift typeset-aarry foriin`seq0$(($#-1))`;do arry[i]=$1 shift done #2、使用变量的间接引用 typeset-aarry foriin`seq1$$#`;do #arry[$((i-1))]=${!i} arry[$((i-1))]=`evalecho\$$i` done
参考文献:
Linux系统命令和Shell脚本实践指南
如有bug或疑问联系:QQ787743742;or mail:787743742@qq.com