首页 > 代码库 > Bash中${}的用法和变量的间接引用
Bash中${}的用法和变量的间接引用
在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 ~]# unset a [root@localhost ~]# echo ${a:-hello};echo $a hello #$a未赋值输出了空值 #可以再脚本用于设置默认值
3、${VAR:=DEFAULT},VAR没有定义或空则为$DEFAULT的值
[root@localhost ~]# echo ${a:=hello};echo a 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 ~]# unset a [root@localhost ~]# echo ${a:+yemo};echo $a
5、${VAR?MSG},VAR未定义则打印$MSG
[root@localhost ~]# unset a [root@localhost ~]# echo ${a?this is null} -bash: a: this is null [root@localhost ~]# typeset -i a [root@localhost ~]# echo ${a?this is null} -bash: a: this is null
6、${VAR:?MSG},VAR未定义或者为空则打印$MSG
[root@localhost ~]# unset a [root@localhost ~]# echo ${a?this is null} -bash: a: this is null [root@localhost ~]# typeset -i a [root@localhost ~]# echo ${a?this is null} -bash: a: this is null
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 ~]# eval echo \$$a 12345
9、${#STR},返回STR长度
[root@localhost ~]# b=12345 [root@localhost ~]# echo ${#b} 5
10、${STR^^},把STR中的所有小写字母转换为大写输出(不改变值)
[root@localhost ~]# unset a b [root@localhost ~]# a="hello Rie Kugimiya" #钉宫理惠老师,声音很萌很可爱 [root@localhost ~]# echo ${a^^} HELLO RIE KUGIMIYA [root@localhost ~]# echo $a hello Rie Kugimiya
11、${STR,,},把STR中的所有大写字母转换为小写输出(不改变值)
[root@localhost ~]# echo ${a,,} hello rie kugimiya [root@localhost ~]# echo $a hello Rie Kugimiya
12、${STR:POSITON},从$POSITON位置提取子串
[root@localhost ~]# echo $a hello Rie Kugimiya [root@localhost ~]# echo ${a:5} Rie Kugimiya #正向取子串 [root@localhost ~]# echo ${a: -5} #反向从末尾开始计数$POSITON位置的子串 imiya
13、${STR:POSITON:LENGTH},从$POSITON位置提取长度为$LENGTH子串
CentOS_7 [root@localhost ~]# echo $a hello Rie Kugimiya [root@localhost ~]# echo ${a:5:6} #从第5个开始向后取6个字符 Rie K [root@localhost ~]# echo ${a:5:-3} #从第五个开始去去掉倒数3个字符 Rie Kugim [root@localhost ~]# echo ${a: -5:-3} #取倒数5个字符去掉倒数3个字符 im #由于减号是前面的关键字所以POSITON前面得加空格 CentOS_6 [root@localhost ~]# a="daisuki Rie Kugimiya" #依旧是可爱的钉宫理惠老师 [root@localhost ~]# echo ${a:5:6} ki Rie [root@localhost ~]# echo ${a:5:-4} -bash: -4: substring expression < 0 [root@localhost ~]# echo ${a: -5:-2} -bash: -2: substring expression < 0 [root@localhost ~]# echo ${a: -5:2} #从-5位置开始去2个字符长度的子串 im
14、${STR#SUBSTR},从$STR头查找匹配,删除最短匹配$SUBSTR的子串
${arry#SUBSTR},对于数组,对每个数组元素单独匹配,执行删除子串操作
[root@localhost ~]# echo ${a} daisuki Rie Kugimiya [root@localhost ~]# echo ${a#*R} ie Kugimiya #从左向右匹配*R匹配到daisuki R,删除并返回剩下的 #数组 [root@localhost ~]# b=(daisuki Rie Kugimiya) [root@localhost ~]# echo ${b[@]} daisuki Rie Kugimiya [root@localhost ~]# echo ${b[@]#*i} #删除了b[0]的dai,b[1]的Ri,b[2]的 Kugi suki e miya
15、${STR##SUBSTR},从$STR头部查找匹配,删除最长匹配$SUBSTR的子串
[root@localhost ~]# echo $a #前面很详细了,后面就不解释了 daisuki Rie Kugimiya [root@localhost ~]# echo ${a##*i} ya [root@localhost ~]# echo ${b[@]##*i} e ya
16、${STR%SUBSTR},从$STR尾部查找匹配,删除最短匹配$SUBSTR的子串
[root@localhost ~]# echo ${a%i*} daisuki Rie Kugim [root@localhost ~]# echo ${b[@]%i*} daisuk R Kugim
17、${STR%%SUBSTR},从$STR尾部查找匹配,删除最长匹配$SUBSTR的子串
[root@localhost ~]# echo ${a%%i*} da [root@localhost ~]# echo ${b[@]%%i*} da R Kug
18、${STR/SUBSTR/REPLACE},使用$REPALCE替换$STR第一个匹配的$SUBSTR
[root@localhost ~]# echo ${a/i/%%/} da%%/suki Rie Kugimiya [root@localhost ~]# echo ${b[@]/i/%%/} da%%/suki R%%/e Kug%%/miya
19、${STR//SUBSTR/REPLACE},使用$REPALCE替换$STR中所有匹配的$SUBSTR
[root@localhost ~]# echo ${a//i/%%/} da%%/suk%%/ R%%/e Kug%%/m%%/ya [root@localhost ~]# echo ${b[@]//i/%%/} da%%/suk%%/ R%%/e Kug%%/m%%/ya
20、${STR/#SUBSTR/REPLACE},$STR以$STR开头的,则用$REPLACE来替换匹配到的$SUBSTR
[root@localhost ~]# echo ${a/#su/%%} daisuki Rie Kugimiya [root@localhost ~]# echo ${a/#dai/%%} %%suki Rie Kugimiya [root@localhost ~]# echo ${b[@]/#K/@} daisuki Rie @ugimiya [root@localhost ~]# echo ${b[@]/#K/$$} daisuki Rie 1723ugimiya [root@localhost ~]# echo ${b[@]/#K/$#} daisuki Rie 0ugimiya [root@localhost ~]# echo ${b[@]/#K/666} daisuki Rie 666ugimiya
21、${STR/%SUBSTR/REPLACE},$STR以$STR结尾的,则用$REPLACE来替换匹配到的$SUBSTR
[root@localhost ~]# echo ${a/%dai/##} daisuki Rie Kugimiya [root@localhost ~]# echo ${a/%miya/##} daisuki Rie Kugi## [root@localhost ~]# echo ${b[@]/%ie/666} daisuki R666 Kugimiya [root@localhost ~]# echo ${b[@]/%ie/$$} daisuki R1723 Kugimiya
Bash函数或者脚本接受连续参数到数组
#1、使用shift typeset -a arry for i in `seq 0 $(($#-1))`;do arry[i]=$1 shift done #2、使用变量的间接引用 typeset -a arry for i in `seq 1 $$#`;do #arry[$((i-1))]=${!i} arry[$((i-1))]=`eval echo \$$i` done
参考文献:
Linux系统命令和Shell脚本实践指南
如有bug或疑问联系:QQ787743742;or mail:787743742@qq.com
本文出自 “庭前夜末空看雪” 博客,请务必保留此出处http://12550795.blog.51cto.com/12540795/1945656
Bash中${}的用法和变量的间接引用