這篇部落格和我之前整理的字串處理有部分重疊
但是主要字串的特換還是有用,就**了一下。**:
表示式含義
$獲取字串 string 的長度
$從字串 string position 處擷取子串
$從字串 string position 處擷取length 長度的子串
$從字串 string 的開頭,刪除最短匹配 substring 的子串
$從字串 string 的開頭,刪除最長匹配 substring 的子串
$從字串 string 的結尾,刪除最短匹配 substring 的子串
$從字串 string 的結尾,刪除最長匹配 substring 的子串
$在字串 string 中,使用 replacement 替換 substring
$在字串 string 中,使用 replacement,代替所有匹配的 substring
$條件替換 : 如果字串 string 的字首匹配 substring , 那麼就用 replacement 來代替匹配到的 substring
$條件替換 : 如果字串 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 $
33zhang@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 $
nickname is xiao mu xi zi
zhang@zhang-virtual-machine:~/desktop$ echo $
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 $
blog nickname is xiao mu xi zi
zhang@zhang-virtual-machine:~/desktop$ echo $
nickname is xiao mu xi zi
zhang@zhang-virtual-machine:~/desktop$ echo $
nickname is xiao mu xi zi
zhang@zhang-virtual-machine:~/desktop$ echo $
my blog nickname is xiao mu xi zi
zhang@zhang-virtual-machine:~$ echo $
my blog nickname is xiao mu xi
zhang@zhang-virtual-machine:~$ echo $
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 $
welcome to,world,hello,linux
zhang@zhang-virtual-machine:~/desktop$ echo $
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 $
hello,world,hello,linux
zhang@zhang-virtual-machine:~/desktop$ echo $
welcome to,world,hello,linux
zhang@zhang-virtual-machine:~/desktop$ echo $
hello,world,hello,php
zhang@zhang-virtual-machine:~/desktop$
表示式含義$
如果 var 沒有被宣告,那麼就以 default 作為其預設值
$如果var沒有被宣告或者其值為空, 那麼就以 default 作為其預設值
$如果var沒有被宣告,那麼就以 default 作為其預設值
$如果var沒有被宣告或者其值為空,那麼就以 default 作為其預設值
$如果var宣告了, 那麼其值就是 othervalue ,否則就為空字串
$如果var被設定了, 那麼其值就是 othervalue ,否則就為空字串
$匹配之前所有以varprefix開頭進行宣告的變數
$匹配之前所有以varprefix開頭進行宣告的變數
zhang@zhang-virtual-machine:~/desktop$ echo $name
zhang@zhang-virtual-machine:~/desktop$ echo $
zhang
zhang@zhang-virtual-machine:~/desktop$ name=
zhang@zhang-virtual-machine:~/desktop$ echo $name
zhang@zhang-virtual-machine:~/desktop$ echo $
zhang
zhang@zhang-virtual-machine:~/desktop$ echo $
zhang@zhang-virtual-machine:~/desktop$ echo $
zhang
zhang@zhang-virtual-machine:~/desktop$
zhang@zhang-virtual-machine:~/desktop$ echo $lastname
zhang@zhang-virtual-machine:~/desktop$ echo $
zhang@zhang-virtual-machine:~/desktop$ lastname='xiao mu xi zi'
zhang@zhang-virtual-machine:~/desktop$ echo $
guoping
zhang@zhang-virtual-machine:~/desktop$ echo $
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 $
varprefixlastname varprefixname
zhang@zhang-virtual-machine:~/desktop$
zhang@zhang-virtual-machine:~$ skilldynamiclanguage='php'
zhang@zhang-virtual-machine:~$ skillstaticlanguage='c++'
zhang@zhang-virtual-machine:~$ echo $
zhang@zhang-virtual-machine:~$ echo $
skilldynamiclanguage skillstaticlanguage
zhang@zhang-virtual-machine:~$
以上所列,有點冷門(即用的比較少,有些系統指令碼偶爾會用到),不過掌握後還是能省去不少麻煩。 shell 字串替換
替換特定字串,原變數並未修改 echo 匹配字元 替換字元 www.google.com echo net 原變數並未修改 替換所有匹配到的字元,應該通過 echo 將net的.替換為 echo 將net的.替換為 只替換開頭匹配的字串 對於add www.xiaomi.com.www的開頭的www...
shell 字串擷取與變數內容替換
從頭開始,將匹配到的最短子串刪除 第乙個匹配到的子串刪除 刪除第1個分隔符及其之前的內容 從頭開始,將匹配到的最長子串刪除 刪除最後1個分隔符及其之前的內容 從尾開始,將匹配到的最短子串刪除 從尾開始,將匹配到的最長子串刪除 替換掉第乙個匹配到的舊字串 替換掉所有匹配到的舊字串 待處理表量 aa a...
Shell字串的替換
字串的替換 1 只替換第1個子串 格式 還以前面的phone變數為例,確認原始值 1.root dyy echo phone 2.13788768897將字串中的第1個8替換為x 1.root dyy echo 2.137x87688972 替換全部子串 格式 將phone字串中的所有8都替換為x ...