1. 程式人生 > 實用技巧 >shell 字串操作 + 變數替換

shell 字串操作 + 變數替換

這篇部落格和我之前整理的字串處理有部分重疊 https://www.cnblogs.com/dalianpai/p/12702237.html

但是主要字串的特換還是有用,就轉載了一下。轉載:https://blog.csdn.net/zhang_referee/article/details/83511123

1.字串操作

表示式 含義
${#string} 獲取字串 string 的長度
${string:position} 從字串 string position 處擷取子串
${string:position:length} 從字串 string position 處擷取length 長度的子串
${string#substring} 從字串 string 的開頭,刪除最短匹配 substring 的子串
${string##substring} 從字串 string 的開頭,刪除最長匹配 substring 的子串
${string%substring} 從字串 string 的結尾,刪除最短匹配 substring 的子串
${string%%substring} 從字串 string 的結尾,刪除最長匹配 substring 的子串
${string/substring/replacement} 在字串 string 中,使用 replacement 替換 substring
${string//substring/replacement} 在字串 string 中,使用 replacement,代替所有匹配的 substring
${string/#substring/replacement} 條件替換 : 如果字串 string 的字首匹配 substring , 那麼就用 replacement 來代替匹配到的 substring
${string/%substring/replacement} 條件替換 : 如果字串 string 的字尾匹配 substring ,那麼就用 replacement 來代替匹配到的 substring

1.獲取長度:

zhang@zhang-virtual-machine:~/Desktop$ string='my blog nickName is xiao mu xi zi'
zhang@zhang-virtual-machine:~/Desktop$ echo $string
my blog nickName is xiao mu xi zi
zhang@zhang-virtual-machine:~/Desktop$ echo ${#string}
33
zhang@zhang-virtual-machine:~/Desktop$ 

2.字串擷取:

zhang@zhang-virtual-machine:~/Desktop$ echo $string
my blog nickName is xiao mu xi zi
zhang@zhang-virtual-machine:~/Desktop$ echo ${string:8}
nickName is xiao mu xi zi
zhang@zhang-virtual-machine:~/Desktop$ echo ${string:8:8}
nickName
zhang@zhang-virtual-machine:~/Desktop$ 

3.字串替換 :

zhang@zhang-virtual-machine:~/Desktop$ echo $string
my blog nickName is xiao mu xi zi
zhang@zhang-virtual-machine:~/Desktop$ echo ${string#my}
blog nickName is xiao mu xi zi
zhang@zhang-virtual-machine:~/Desktop$ echo ${string#my blog}
nickName is xiao mu xi zi
zhang@zhang-virtual-machine:~/Desktop$ echo ${string##my blog}
nickName is xiao mu xi zi
zhang@zhang-virtual-machine:~/Desktop$ echo ${string##zi}
my blog nickName is xiao mu xi zi
zhang@zhang-virtual-machine:~$ echo ${string%zi}
my blog nickName is xiao mu xi
zhang@zhang-virtual-machine:~$ echo ${string%%xi zi}
my blog nickName is xiao mu
zhang@zhang-virtual-machine:~$ 
zhang@zhang-virtual-machine:~/Desktop$ greet='hello,world,hello,linux'
zhang@zhang-virtual-machine:~/Desktop$ echo $greet
hello,world,hello,linux
zhang@zhang-virtual-machine:~/Desktop$ echo ${greet/hello/welcome to}
welcome to,world,hello,linux
zhang@zhang-virtual-machine:~/Desktop$ echo ${greet//hello/welcome to}
welcome to,world,welcome to,linux
zhang@zhang-virtual-machine:~/Desktop$ echo $greet
hello,world,hello,linux
zhang@zhang-virtual-machine:~/Desktop$ echo $greet
hello,world,hello,linux
zhang@zhang-virtual-machine:~/Desktop$ echo ${greet/#world/php}
hello,world,hello,linux
zhang@zhang-virtual-machine:~/Desktop$ echo ${greet/#hello/welcome to}
welcome to,world,hello,linux
zhang@zhang-virtual-machine:~/Desktop$ echo ${greet/%linux/php}
hello,world,hello,php
zhang@zhang-virtual-machine:~/Desktop$ 

2.變數替換

表示式 含義
${var-DEFAULT} 如果 var 沒有被宣告,那麼就以 DEFAULT 作為其預設值
${var:-DEFAULT} 如果var沒有被宣告或者其值為空, 那麼就以 DEFAULT 作為其預設值
${var=DEFAULT} 如果var沒有被宣告,那麼就以 DEFAULT 作為其預設值
${var:=DEFAULT} 如果var沒有被宣告或者其值為空,那麼就以 DEFAULT 作為其預設值
${var+OTHERVALUE} 如果var聲明瞭, 那麼其值就是 OTHERVALUE ,否則就為空字串
${var:+OTHERVALUE} 如果var被設定了, 那麼其值就是 OTHERVALUE ,否則就為空字串
${!varprefix*} 匹配之前所有以varprefix開頭進行宣告的變數
${!varprefix@} 匹配之前所有以varprefix開頭進行宣告的變數
zhang@zhang-virtual-machine:~/Desktop$ echo $name
zhang@zhang-virtual-machine:~/Desktop$ echo ${name-zhang}
zhang
zhang@zhang-virtual-machine:~/Desktop$ name=
zhang@zhang-virtual-machine:~/Desktop$ echo $name
zhang@zhang-virtual-machine:~/Desktop$ echo ${name:-zhang}
zhang
zhang@zhang-virtual-machine:~/Desktop$ echo ${name=zhang}
zhang@zhang-virtual-machine:~/Desktop$ echo ${name:=zhang}
zhang
zhang@zhang-virtual-machine:~/Desktop$ 
zhang@zhang-virtual-machine:~/Desktop$ echo $lastName
zhang@zhang-virtual-machine:~/Desktop$ echo ${lastName+guoping}
zhang@zhang-virtual-machine:~/Desktop$ lastName='xiao mu xi zi'
zhang@zhang-virtual-machine:~/Desktop$ echo ${lastName+guoping}
guoping
zhang@zhang-virtual-machine:~/Desktop$ echo ${lastName:+guoping}
guoping
zhang@zhang-virtual-machine:~/Desktop$ 
zhang@zhang-virtual-machine:~/Desktop$ varprefixName='zhang'
zhang@zhang-virtual-machine:~/Desktop$ varprefixLastName='guoping'
zhang@zhang-virtual-machine:~/Desktop$ fullName='guoping zhang'
zhang@zhang-virtual-machine:~/Desktop$ echo ${!varprefix*}
varprefixLastName varprefixName
zhang@zhang-virtual-machine:~/Desktop$ 
zhang@zhang-virtual-machine:~$ skillDynamicLanguage='php'
zhang@zhang-virtual-machine:~$ skillStaticLanguage='C++'
zhang@zhang-virtual-machine:~$ echo ${!Lanugage@}
zhang@zhang-virtual-machine:~$ echo ${!skill@}
skillDynamicLanguage skillStaticLanguage
zhang@zhang-virtual-machine:~$ 

以上所列,有點冷門(即用的比較少,有些系統指令碼偶爾會用到),不過掌握後還是能省去不少麻煩。