一、变量内容的删除、取代和替换
(1)变量内容的删除与取代
变量设定方式 | 说明 |
${变量#关键词} | 若变量内容从头开始的数据符合关键词,则将符合的最短数据删除 |
${变量##关键词} | 若变量内容从头开始的数据符合关键词,则将符合的最长数据删除 |
${变量%关键词} | 若变量内容从尾向前的数据符合关键词,则将符合的最短数据删除 |
${变量%%关键词} | 若变量内容从尾向前的数据符合关键词,则将符合的最长数据删除 |
${变量/旧字符串/新字符串} | 若变量内容符合旧字符串则第一个旧字符串被新字符串取代 |
${变量//旧字符串/新字符串} | 若变量内容符合旧字符串则全部旧字符串会被新字符串取代 |
[dalianmao@localhost ~]$ path=$PATH
[dalianmao@localhost ~]$ echo $path
/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/dalianmao/bin
[dalianmao@localhost ~]$ echo ${path#/*:}
/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/dalianmao/bin
[dalianmao@localhost ~]$ echo ${path##/*:}
/home/dalianmao/bin
[dalianmao@localhost ~]$ echo ${path%/*:}
/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/dalianmao/bin
[dalianmao@localhost ~]$ echo ${path%:/*}
/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin
[dalianmao@localhost ~]$ echo ${path%%:/*}
/usr/local/bin
[dalianmao@localhost ~]$ echo ${path/bin/BIN}
/usr/local/BIN:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/dalianmao/bin
[dalianmao@localhost ~]$ echo ${path//bin/BIN}
/usr/local/BIN:/BIN:/usr/BIN:/usr/local/sBIN:/usr/sBIN:/sBIN:/home/dalianmao/BIN
[dalianmao@localhost ~]$
(2)变量的测试与内容替换
变量设定方式 | str没有设定 | str为空字符串 | str 已设定非为空的字符串 |
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 不变 var= | str不变 var=$str |
var=${str:=expr} | str=expr var=expr | str=expr var=expr | str不变 var=$str |
var=${str?expr} | expr输出至stderr | var= | var=$str |
var=${str:?expr} | expr输出至stderr | expr输出至stderr | var=$str |
eg1:
[dalianmao@localhost ~]$ clear
[dalianmao@localhost ~]$ unset str;var=${str-dalianmao}
[dalianmao@localhost ~]$ echo $var;echo $str
dalianmao
[dalianmao@localhost ~]$ unset str;var=${str:-dalianmao}
[dalianmao@localhost ~]$ echo $var;echo $str
dalianmao
[dalianmao@localhost ~]$ str=xianyu;var=${str-dalianmao}
[dalianmao@localhost ~]$ echo $var;echo $str
xianyu
xianyu
[dalianmao@localhost ~]$ str=xianyu;var=${str:-dalianmao}
[dalianmao@localhost ~]$ echo $var;echo $str
xianyu
xianyu
[dalianmao@localhost ~]$ str='';var=${str-dalianmao}
[dalianmao@localhost ~]$ echo $var;echo $str
[dalianmao@localhost ~]$ str='';var=${str:-dalianmao}
[dalianmao@localhost ~]$ echo $var;echo $str
dalianmao
[dalianmao@localhost ~]$
eg2.
[dalianmao@localhost ~]$ unset str;var=${str+dalianmao}
[dalianmao@localhost ~]$ echo $var;echo $str
[dalianmao@localhost ~]$ unset str;var=${str:+dalianmao}
[dalianmao@localhost ~]$ echo $var;echo $str
[dalianmao@localhost ~]$ str='';var=${str+dalianmao};var1=${str:+dalianmao}
[dalianmao@localhost ~]$ echo $var;echo $var1;echo $str
dalianmao
[dalianmao@localhost ~]$ str=xianyu;var=${str+dalianmao};var1=${str:+dalianmao}
[dalianmao@localhost ~]$ echo $var;echo $var1;echo $str
dalianmao
dalianmao
xianyu
eg3.
[dalianmao@localhost ~]$ unset str;var=${str=dalianmao}
[dalianmao@localhost ~]$ echo $var;echo $str
dalianmao
dalianmao
[dalianmao@localhost ~]$ unset str;var1=${str:=dalianmao}
[dalianmao@localhost ~]$ echo $var1;echo $str
dalianmao
dalianmao
[dalianmao@localhost ~]$ str='';var=${str=dalianmao}
[dalianmao@localhost ~]$ echo $var;echo $str
[dalianmao@localhost ~]$ str='';var1=${str:=dalianmao}
[dalianmao@localhost ~]$ echo $var1;echo $str
dalianmao
dalianmao
[dalianmao@localhost ~]$ str=xianyu;var=${str=dalianmao}
[dalianmao@localhost ~]$ echo $var;echo $str
xianyu
xianyu
[dalianmao@localhost ~]$ str=xianyu;var1=${str=dalianmao}
[dalianmao@localhost ~]$ echo $var1;echo $str
xianyu
xianyu
[dalianmao@localhost ~]$ str=xiayu;var1=${str:=dalianmao}
[dalianmao@localhost ~]$ echo $var1;echo $str
xiayu
xiayu
eg4.
[dalianmao@localhost ~]$ unset str;var=${str?dalianmao}
-bash: str: dalianmao
[dalianmao@localhost ~]$ unset str;var1=${str:?dalianmao}
-bash: str: dalianmao
[dalianmao@localhost ~]$ str='';var=${str?dalianmao}
[dalianmao@localhost ~]$ echo $var;echo $str
[dalianmao@localhost ~]$ str='';var1=${str:?dalianmao}
-bash: str: dalianmao
[dalianmao@localhost ~]$ str=xianyu;var=${str?dalianmao}
[dalianmao@localhost ~]$ echo $var;echo $str
xianyu
xianyu
[dalianmao@localhost ~]$ str=xianyu;var1=${str:?dalianmao}
[dalianmao@localhost ~]$ echo $var1
xianyu
二、通配符与特殊符号
(1)通配符
符号 | 意义 |
* | 代表0个到无穷多个任意字符 |
? | 代表一定有一个任意字符 |
[] | 同样代表一定有一个在括号内的字符(非任意字符)。例如abcd代表一定有一个字符,可能是a,b,c,d这四个任何一个 |
[-] | 若有减号在中括号内时,代表在编码顺序内的所有字符。例如[0-9]代表0到9之间的所有数字,因为数字的语系编码是连续的! |
[^] | 若中括号内的第一个字符为指数符号(^),那表示反向选择,例如[^abc]代表一定有一个字符,只要是非a,b,c的其他字符就接受的意思。 |
(2)特殊符号
符号 | 内容 |
# | 批注符号 |
\ | 跳脱符号 |
| | 管线 |
; | 连续指令下达分隔符 |
~ | 用户的家目录 |
$ | 取用变数前导符:亦即是变量之前需要加的变量取代值 |
& | 工作控制:将指令变成背景下工作 |
! | 逻辑运算意义上的非 not的意思! |
/ | 目录符号:路径分隔的符号 |
> , >> | 数据流重导向:输出导向,分别是取代与累加 |
< , << | 数据流重导向 |
' ' | 单引号,不具有变量置换的功能 |
" " | 具有变量置换的功能! |
` ` | 两个 ` 中间为可以先执行的指令,亦可使用$() |
() | 在中间为子shell的起始与结束 |
{} | 在中间为命令区块的组合! |