linux变量操作

来源:互联网 发布:装安卓平面设计软件 编辑:程序博客网 时间:2024/05/18 01:20
linux系统变量有很多快捷操作,如下所示:
[root@nothing~]# var="st"[root@nothing~]# echo $varst[root@nothing~]# unset $exp[root@nothing~]# att=${exp:-$var}[root@nothing~]# echo $attst

下面总结了一些变量快捷操作的方法:

變數設定方式說明${變數#關鍵字}
${
變數##關鍵字}若變數內容從頭開始的資料符合『關鍵字』,則將符合的最短資料刪除
若變數內容從頭開始的資料符合『關鍵字』,則將符合的最長資料刪除${變數%關鍵字}
${
變數%%關鍵字}若變數內容從尾向前的資料符合『關鍵字』,則將符合的最短資料刪除
若變數內容從尾向前的資料符合『關鍵字』,則將符合的最長資料刪除${變數/舊字串/新字串}
${
變數//舊字串/新字串}若變數內容符合『舊字串』則『第一個舊字串會被新字串取代』


變數設定方式str 沒有設定str 為空字串str 已設定非為空字串var=${str-expr}var=exprvar=var=$strvar=${str:-expr}var=exprvar=exprvar=$strvar=${str+expr}var=var=exprvar=exprvar=${str:+expr}var=var=var=exprvar=${str=expr}str=expr
var=exprstr 不變
var=str 不變
var=$strvar=${str:=expr}str=expr
var=exprstr=expr
var=exprstr 不變
var=$strvar=${str?expr}expr 輸出至 stderrvar=var=$strvar=${str:?expr}expr 輸出至 stderrexpr 輸出至 stderrvar=$str

以后在慢慢总结更多的用法
原创粉丝点击