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

shell 字串操作 + 變數替換

本篇內容包括:

  1.     字串操作
  2.     變數替換

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.獲取長度:

[email protected]:~/Desktop$ string='my blog nickName is xiao mu xi zi'
[email protected]:~/Desktop$ echo $string
my blog nickName is xiao mu xi zi
[email protected]:~/Desktop$ echo ${#string}
33
[email protected]:~/Desktop$ 

   2.字串擷取:

[email protected]:~/Desktop$ echo $string
my blog nickName is xiao mu xi zi
[email protected]:~/Desktop$ echo ${string:8}
nickName is xiao mu xi zi
[email protected]:~/Desktop$ echo ${string:8:8}
nickName
[email protected]:~/Desktop$ 

  3.字串替換 :

[email protected]:~/Desktop$ echo $string
my blog nickName is xiao mu xi zi
[email protected]:~/Desktop$ echo ${string#my}
blog nickName is xiao mu xi zi
[email protected]:~/Desktop$ echo ${string#my blog}
nickName is xiao mu xi zi
[email protected]:~/Desktop$ echo ${string##my blog}
nickName is xiao mu xi zi
[email protected]:~/Desktop$ echo ${string##zi}
my blog nickName is xiao mu xi zi
[email protected]:~$ echo ${string%zi}
my blog nickName is xiao mu xi
[email protected]:~$ echo ${string%%xi zi}
my blog nickName is xiao mu
[email protected]:~$ 

 

[email protected]:~/Desktop$ greet='hello,world,hello,linux'
[email protected]:~/Desktop$ echo $greet
hello,world,hello,linux
[email protected]:~/Desktop$ echo ${greet/hello/welcome to}
welcome to,world,hello,linux
[email protected]:~/Desktop$ echo ${greet//hello/welcome to}
welcome to,world,welcome to,linux
[email protected]:~/Desktop$ echo $greet
hello,world,hello,linux

[email protected]:~/Desktop$ echo $greet
hello,world,hello,linux
[email protected]:~/Desktop$ echo ${greet/#world/php}
hello,world,hello,linux
[email protected]:~/Desktop$ echo ${greet/#hello/welcome to}
welcome to,world,hello,linux
[email protected]:~/Desktop$ echo ${greet/%linux/php}
hello,world,hello,php
[email protected]:~/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開頭進行宣告的變數
${[email protected]} 匹配之前所有以varprefix開頭進行宣告的變數
[email protected]:~/Desktop$ echo $name

[email protected]:~/Desktop$ echo ${name-zhang}
zhang
[email protected]:~/Desktop$ name=
[email protected]:~/Desktop$ echo $name

[email protected]:~/Desktop$ echo ${name:-zhang}
zhang
[email protected]:~/Desktop$ echo ${name=zhang}

[email protected]:~/Desktop$ echo ${name:=zhang}
zhang
[email protected]:~/Desktop$ 

 

[email protected]:~/Desktop$ echo $lastName

[email protected]:~/Desktop$ echo ${lastName+guoping}

[email protected]:~/Desktop$ lastName='xiao mu xi zi'
[email protected]:~/Desktop$ echo ${lastName+guoping}
guoping
[email protected]:~/Desktop$ echo ${lastName:+guoping}
guoping
[email protected]:~/Desktop$ 

 

[email protected]:~/Desktop$ varprefixName='zhang'
[email protected]:~/Desktop$ varprefixLastName='guoping'
[email protected]:~/Desktop$ fullName='guoping zhang'
[email protected]:~/Desktop$ echo ${!varprefix*}
varprefixLastName varprefixName
[email protected]:~/Desktop$ 
[email protected]:~$ skillDynamicLanguage='php'
[email protected]:~$ skillStaticLanguage='C++'
[email protected]:~$ echo ${[email protected]}

[email protected]:~$ echo ${[email protected]}
skillDynamicLanguage skillStaticLanguage
[email protected]:~$ 

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