linux shell數組操作

1、定義數組
說明:一對括號表示是數組,數組元素用“空格”符號分割開。
實例:
[test@test]$ a=(1 2 3 4 5)
[test@test]$ echo $a
1

2、讀取數組元素
形式:
(1)讀取某個元素
[test@test]$ a=(1 2 3 4 5)
[test@test]$ echo ${a[2]} 
3
(2)讀取全部元素
[test@test]$ a=(1 2 3 4 5)
[test@test]$ echo ${a[*]} 
1 2 3 4 5
3、刪除數組元素
說明:使用unset命令
形式: 
[test@test]$ a=(1 2 3 4 5)
[test@test]$ unset a[1]   
[test@test]$ echo ${a[*]} 
1 3 4 5
4、數組元素賦值
形式:
[test@test]$ a=(1 2 3 4 5)
[test@test]$ a[1] = 100
[test@test]$ echo ${a[*]} 
1 100 3 4 5

5、獲得數組長度

形式:length =${#a[@]}

[test@test]$ a=(1 2 3 4 5)

[test@test]$ echo   ${#a[@]}

5


發佈了29 篇原創文章 · 獲贊 2 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章