【基本介绍】
这里我们介绍bash里面对字符串,变量等的替换等操作。
【基本用法】
shell parameter substitution
1. ${parameter}
2. ${parameter-default} ${parameter:-default}
3. ${parameter=default} ${parameter:=default}
4. ${parameter+alt_value} ${parameter:+alt_value}
5. ${parameter?err_msg} ${parameter:?err_msg}
6. ${#parameter}
7. ${var#pattern} ${var##pattern}
8. ${var%pattern} ${var%%pattern}
9. ${var:pos} ${var:pos:len}
10. ${var/pattern/replacement} ${var//pattern/replacement} ${var/#Pattern/Replacement} ${var/%Pattern/Replacement}
11. ${!varprefix*}, ${!varprefix@}
1. 变量值 - 不解释了
[root@sparkVM tmp]# cat 1.sh
parameter='test'
echo $parameter #test
echo ${parameter} #test
2. 默认值
[root@sparkVM tmp]# cat 2.sh
echo ${parameter-default} #变量没有定义的时候使用默认值
echo ${parameter:-default}
echo ${parameter=default}
echo ${parameter:=default}
parameter=
echo ${parameter-default} #变量定义了,但是是空值,
echo ${parameter:-default} # :- :=都继续用默认值 , 其他就是空值
echo ${parameter=default}
echo ${parameter:=default}
parameter='test' #变量定义了值,都使用变量的值
echo ${parameter-default}
echo ${parameter:-default}
echo ${parameter=default}
echo ${parameter:=default}
[root@sparkVM tmp]# bash 2.sh
default
default
default
default
default
default
test
test
test
test
3. 有值就取默认,没有定义就空
[root@sparkVM tmp]# cat 3.sh
echo 1, ${parameter+default} #没有定义变量,输出空
echo 2, ${parameter:+default}
parameter=
echo 3, ${parameter+default} #定义了变量,但是为空
echo 4, ${parameter:+default} #:+还是使用原来值,+使用默认值
parameter='test'
echo 5, ${parameter+default} #定义了变量,不为空
echo 6, ${parameter:+default} #使用默认值
[root@sparkVM tmp]# bash 3.sh
1,
2,
3, default
4,
5, default
6, default
4. 有值无值判断
[root@sparkVM tmp]# cat 4.sh
parameter='test'
echo 1, ${parameter?default} #变量值定义 不为空,输出变量值
echo 2, ${parameter:?default}
parameter=
echo 3, ${parameter?default} #变量值定义,为空, ?输出变量值,:?判断为无变量值
#echo ${parameter:?default}
unset parameter
#echo 4, ${parameter?default} #无变量值,报错提示:default
#echo 5, ${parameter:?default}
[root@sparkVM tmp]# bash 4.sh
1, test
2, test
3,
5.变量长度 - 不解释
[root@sparkVM tmp]# cat 5.sh
echo ${#parameter}
parameter=
echo ${#parameter}
parameter='test'
echo ${#parameter}
[root@sparkVM tmp]# bash 5.sh
0
0
4
6. 变量截取
[root@sparkVM tmp]# cat 6.sh
var1=abcd12345abc6789
echo ${var1}
echo ${var1#a*c} #‘#’是从左往右截取 #最短匹配 ##最长匹配
echo ${var1##a*c}
echo ${var1%a*c} #‘#/%’如果最左或者最右开头没有匹配的,那么输出源
echo ${var1%%a*c}
echo ${var1%b*9} # ‘%’是从右往左截取 %最短匹配 %%最长匹配
echo ${var1%%b*9}
[root@sparkVM tmp]# bash 6.sh
abcd12345abc6789
d12345abc6789
6789
abcd12345abc6789
abcd12345abc6789
abcd12345a
a
7. 变量长度截取
[root@sparkVM tmp]# cat 7.sh
var='abcdefghijklmn'
echo ${var:2} #索引从2开始到结尾,初始为0
echo ${var:2:5} #索引从2开始,长度为5
[root@sparkVM tmp]# bash 7.sh
cdefghijklmn
cdefg
8. 变量替换
[root@sparkVM tmp]# cat 8.sh
var='abcdefghijklmncdefj'
echo ${var/cde/CDE} #${var/Pattern/Replacement} 替换第一次匹配
echo ${var//cde/CDE} #${var//Pattern/Replacement} 全局替换
echo ${var/#abc/ABC} #‘#’从最左边开始匹配
echo ${var/%efj/EFJ} # '%'从最右边开始匹配
echo ${var/#cde/CDE}
echo ${var/%cde/CDE} #最右边没有匹配,输出源
[root@sparkVM tmp]# bash 8.sh
abCDEfghijklmncdefj
abCDEfghijklmnCDEfj
ABCdefghijklmncdefj
abcdefghijklmncdEFJ
abcdefghijklmncdefj
abcdefghijklmncdefj
9. 变量匹配
[root@sparkVM tmp]# cat 9.sh
# This is a variation on indirect reference, but with a * or @.
# Bash, version 2.04, adds this feature.
xyz23=whatever
xyz24=
a=${!xyz*} # Expands to *names* of declared variables
# ^ ^ ^ + beginning with "xyz".
echo "a = $a" # a = xyz23 xyz24
a=${!xyz@} # Same as above.
echo "a = $a" # a = xyz23 xyz24
echo "---"
abc23=something_else
b=${!abc*}
echo "b = $b" # b = abc23
c=${!b} # Now, the more familiar type of indirect reference.
echo $c # something_else
[root@sparkVM tmp]# bash 9.sh
a = xyz23 xyz24
a = xyz23 xyz24
---
b = abc23
something_else
【参考引用】
http://tldp.org/LDP/abs/html/parameter-substitution.html#PARAMSUBREF