Deletion of shell variables:
[Email protected]:~$ Path=${path} # #设置path和PATH相同内容 [email protected]:~$ echo $path/usr/local/sbin:/usr/local/bin:/ Usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games[email protected]:~$ echo ${path#/*sbin:} # #加上 ' # ' symbol delete Match '/* Sbin: ' Minimum combination/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games[email protected]:~$ echo ${ Path##/*sbin:} # #加上 ' # # ' delete match '/*sbin: ' Maximum combination/bin:/usr/games:/usr/local/games
Variable Setting method |
Description |
${variable # keyword} |
Delete the shortest combination if the variable content starts with the data in the keyword |
${variable # #关键字} |
Delete the longest combination if the variable content starts with the "keyword" data |
${Variable% keyword} |
Delete the shortest combination if the variable content starts at the end of the data that matches the keyword |
${Variable% keyword} |
Delete the longest combination if the variable content starts at the end of the data that matches the keyword |
${variable/old string/new string} |
Replace the first old string with a new string if the contents of the variable conform to the old string |
${variable//old string//new String} |
Replace the first old string with a new string if the contents of the variable conform to the old string |
Substitution of shell variables:
[Email protected]:~$ Path=${path} # #设置path和PATH相同内容 [email protected]:~$ echo $path/usr/local/sbin:/usr/local/bin:/ Usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games[email protected]:~$ Echo ${path/sbin/newsbin} # # Replace the first sbin with a newsbin/usr/local/newsbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games[ Email protected]:~$ Echo ${path//sbin//newsbin} # #将所有的sbin替换成newsbin/usr/local//newsbin:/usr/local/bin:/usr// Newsbin:/usr/bin://newsbin:/bin:/usr/games:/usr/local/games
Substitution of shell variables:
How variables are set
|
Str not set
|
STR is empty
|
STR contains content
|
VAR=${STR-EXPR}
|
var=expr
|
Var= |
Var= $str
|
VAR=${STR:-EXPR} |
var=expr |
var=expr |
Var= $str |
VAR=${STR+EXPR} |
Var=
|
var=expr |
var=expr |
VAR=${STR:+EXPR} |
Var= |
Var= |
var=expr |
VAR=${STR=EXPR} |
str=expr var=expr |
Str= not change var= |
str= unchanged var= $str |
VAR=${STR:=EXPR} |
str=expr var=expr |
str=expr var=expr |
str= unchanged var= $str |
VAR=${STR?EXPR} |
Expr output to stderr
|
Var= |
Var=str
|
VAR=${STR:?EXPR} |
Expr output to stderr |
Expr output to stderr |
Var=str |
Deletion, substitution and substitution of shell variable contents