shell————-數組

來源:互聯網
上載者:User

先看一個例子:

#!/bin/basharea2=(zero one two three four)echo "Origin is :"echo ${area2[0]} ${area2[1]} ${area2[2]} ${area2[3]} ${area2[4]}area2[1]=1area2[4]=4echo "After is :"echo ${area2[0]} ${area2[1]} ${area2[2]} ${area2[3]} ${area2[4]}echo ${#area2}echo "One way to output the array:"echo ${area2[@]}

輸出:

root@vivi-Ideapad-Z460:~# ./myshell.sh
Origin is :
zero one two three four
After is :
zero 1 two three 4
4
One way to output the array:
zero 1 two three 4
root@vivi-Ideapad-Z460:~# 

#!/bin/basharea2=(zero one two three four)echo "Origin is :"echo ${area2[@]}echo ${area2[@]:0} # 提取尾部的子串echo ${area2[@]:1}echo ${area2[@]:1:2}


root@vivi-Ideapad-Z460:~# ./myshell.sh
Origin is :
zero one two three four
zero one two three four
one two three four
one two
root@vivi-

#
子串刪除

#
從字串的前部刪除最短的匹配,

#+匹配字串是一個Regex.

echo${arrayZ[@]#f*r} # one two three five five

#
匹配運算式作用於數組所有元素.

#
匹配了"four"並把它刪除.

#字串前部最長的匹配

echo${arrayZ[@]##t*e} # one two four five five

#
匹配運算式作用於數組所有元素.

#
匹配"three"並把它刪除.

#字串尾部的最短匹配

echo${arrayZ[@]%h*e} # one two t four five five

#
匹配運算式作用於數組所有元素.

#
匹配"hree"並把它刪除.

#字串尾部的最長相符

echo${arrayZ[@]%%t*e} # one two four five five

#
匹配運算式作用於數組所有元素.

#
匹配"three"並把它刪除.

#子串替換

#第一個匹配的子串會被替換

echo${arrayZ[@]/fiv/XYZ} # one two three four XYZe XYZe

#
匹配運算式作用於數組所有元素.

#所有匹配的子串會被替換

echo${arrayZ[@]//iv/YY} # one two three four fYYe fYYe

#
匹配運算式作用於數組所有元素.

#刪除所有的匹配子串

#沒有指定代替字串意味著刪除

echo${arrayZ[@]//fi/} # one two three four ve ve

#
匹配運算式作用於數組所有元素.

#替換最前部出現的字串

echo${arrayZ[@]/#fi/XY} # one two three four XYve XYve

#
匹配運算式作用於數組所有元素.

#替換最後部出現的字串

echo${arrayZ[@]/%ve/ZZ} # one two three four fiZZ fiZZ

#
匹配運算式作用於數組所有元素.

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.