1. Bash中的参数扩展
参数是一个存储数值的实体,并由名称、数字或特定符号所引用:
被名称引用的参数称作变量;
被数字引用的参数称作位置参数;
被特定符号引用的参数具有特殊的含义和用途,被作为Bash的特殊内部变量引用。
参数扩展是从引用的实体取值的过程,就像扩展变量打印它的值。
1)基本的参数扩展
$PARAMETER ${PARAMETER} #当参数名后面紧接着其他字符,必须使用{} $ WORD=car $ echo $WORDs $ echo ${WORD}s cars #位置参数9以后的,也必须加{} $ echo "Arguments 10 is $1" $ echo "Arguments 10 is ${10}"
2)间接参数扩展
$ PARAMETER=TEMP $ TEMP="It‘s indirect" $ echo ${PARAMETER} #PARAMETER的值 TEMP $ echo ${!PARAMETER} #扩展为参数TEMP的值 It‘s indirect
3)大小写修改(Bash 4.0的新特性)
$ PARAMETER=tEmp $ echo ${PARAMETER^} #首字母大写 TEmp $ echo ${PARAMETER^^} #所有字母大写 TEMP $ echo ${PARAMETER,} #首字母小写 tEmp $ echo ${PARAMETER,,} #所有字母小写 temp $ echo ${PARAMETER~} #首字母大小写转换 TEmp $ echo ${PARAMETER~~} #所有字母大小写转换 TeMP
3)变量名扩展
${!PREFIX*} #输出以字符串PREFIX开头的所有变量名,默认以空格隔开 ${!PREFIX@} #示例 $ echo ${!BASH*} BASH BASHOPTS BASHPID BASH_ALIASES BASH_ARGC BASH_ARGV BASH_CMDS BASH_COMMAND BASH_LINENO BASH_SOURCE BASH_SUBSHELL BASH_VERSINFO BASH_VERSION $ echo ${!BASH@} BASH BASHOPTS BASHPID BASH_ALIASES BASH_ARGC BASH_ARGV BASH_CMDS BASH_COMMAND BASH_LINENO BASH_SOURCE BASH_SUBSHELL BASH_VERSINFO BASH_VERSION
4)字符串移除
${PARAMETER#PATTERN} #移除参数值字符中开头匹配模式的最短文本 ${PARAMETER##PATTERN} #移除参数值字符中开头匹配模式的最长文本 ${PARAMETER%PATTERN} #移除参数值字符中末尾匹配模式的最短文本 ${PARAMETER%%PATTERN} #移除参数值字符中末尾匹配模式的最长文本 #示例: $ MYSTRING="This is used for removing string" $ echo ${MYSTRING} This is used for removing string $ echo ${MYSTRING#* } is used for removing string $ echo ${MYSTRING##* } string $ echo ${MYSTRING% *} This is used for removing $ echo ${MYSTRING%% *} This #常用场景:提取文件名的一部分 $ FILENAME=echotime.sh $ echo ${FILENAME} echotime.sh $ echo ${FILENAME##*.} #提取后缀 sh $ echo ${FILENAME%.*} #移除后缀 echotime $ echo ${FILENAME} /home/user/echotime.sh $ echo ${FILENAME##*/} #提取文件名 echotime.sh $ echo ${FILENAME%/*} #提取目录名 /home/user
5)字符串搜索与替换
${PARAMETER/PATTEN/STRING} #替换一个匹配指定模式的子字符串 ${PARAMETER//PATTEN/STRING} #替换所有匹配指定模式的子字符串 ${PARAMETER/PATTEN} #不指定替换的字符串,则为删除一个匹配指定模式的子字符串 ${PARAMETER//PATTEN} #不指定替换的字符串,则为删除一个匹配指定模式的子字符串 $ MYSTRING="This is used for replacing string or removing string" $ echo ${MYSTRING} This is used for replacing string or removing string $ echo ${MYSTRING/string/characters} #替换一个 This is used for replacing characters or removing string $ echo ${MYSTRING//string/characters} #替换所有 This is used for replacing characters or removing characters $ echo ${MYSTRING/string} #删除一个 This is used for replacing or removing string $ echo ${MYSTRING//string} #删除所有 This is used for replacing or removing
6)字符串长度
${#PARAMETER} #获取字符串的长度 $ MYSTRING="Hello World" $ echo ${#MYSTRING} 11
7)子字符串扩展
${PAREMETER:OFFSET} #从指定位置截取到参数值的末尾 ${PAREMETER:OFFSET:LENGTH} #从指定位置截取指定长度的字符串 $ MYSTRING="This is used for substring expansion" $ echo ${MYSTRING} This is used for substring expansion $ echo ${MYSTRING:8} #未指定长度 used for substring expansion $ echo ${MYSTRING:8:13} #指定长度 used for subs
8)使用默认值
${PAREMETER:-WORD} #如果PAREMETER是未定义或者null时,扩展WORD,否则扩展PAREMETER ${PAREMETER-WORD} #与上面不同:只有PAREMETER是未定义时,才使用WORD $ unset MYSTRING #未定义 $ echo ${MYSTRING} $ echo ${MYSTRING:-Hello World} #未定义 Hello World $ echo ${MYSTRING} $ MYSTRING="Hi" $ echo ${MYSTRING:-Hello World} #定义 Hi
9)指定默认值
${PAREMETER:=WORD} #如果PAREMETER是未定义或者null时,扩展WORD,并将WORD赋值给PAREMETER ${PAREMETER=WORD} #与上面不同:只有PAREMETER是未定义时,才使用WORD,并将WORD赋值给PAREMETER $ unset MYSTRING $ echo ${MYSTRING} $ echo ${MYSTRING:=Hello World} #未定义 Hello World $ echo ${MYSTRING} #赋值 Hello World $ MYSTRING="Hi" $ echo ${MYSTRING:=Hello World} #已定义 Hi $ echo ${MYSTRING} #不赋值 Hi
10)使用替代值
${PAREMETER:+WORD} #如果PAREMETER是未定义或者null时,不扩展任何内容;如果PAREMETER是已定义且不为空,则扩展WORD,而不是PARAMETER的值 ${PAREMETER+WORD} #与上面不同:#如果PAREMETER是未定义时,不扩展任何内容;如果PAREMETER是已定义,则扩展WORD $ unset MYSTRING $ echo ${MYSTRING} $ echo ${MYSTRING:+NOTE: MYSTRINT seems to be set} #未定义,不扩展任何内容 $ MYSTRING="Hi" $ echo ${MYSTRING} Hi $ echo ${MYSTRING:+NOTE: MYSTRINT seems to be set} #已定义,扩展WORD而不是PARAMETER NOTE: MYSTRINT seems to be set
11)显示错误信息
${PAREMETER:?WORD} #如果PAREMETER是未定义或者null时,返回一个错误,以WORD为错误信息,否则扩展PARAMETER的值 ${PAREMETER?WORD} #与上面不同:如果PAREMETER是未定义时,返回一个错误,以WORD为错误信息,否则扩展PAREMETER,为空则返回空 $ unset MYSTRING $ echo ${MYSTRING} $ echo ${MYSTRING:?Error: MYSTRINT is not set} -bash: MYSTRING: Error: MYSTRINT is not set $ MYSTRING="Hi" $ echo ${MYSTRING:?Error: MYSTRINT is not set} Hi
本节结束