Linux shell陣列建立和使用技巧

2021-09-02 10:21:10 字數 948 閱讀 1872

1.陣列定義

[chengmo@centos5 ~]$ a=(1 2 3 4 5)

[chengmo@centos5 ~]$ echo $a 1

一對括號表示是陣列,陣列元素用「空格」符號分割開。

2.陣列讀取與賦值

得到長度:

[chengmo@centos5 ~]$ echo $ 5

用$ 可以得到陣列長度

讀取:[chengmo@centos5 ~]$ echo $ 3

[chengmo@centos5 ~]$ echo $

1 2 3 4 5

用$ 下標是從0開始  下標是:*或者@ 得到整個陣列內容

賦值:[chengmo@centos5 ~]$ a[1]=100

[chengmo@centos5 ~]$ echo $

1 100 3 4 5

[chengmo@centos5 ~]$ a[5]=100

[chengmo@centos5 ~]$ echo $

1 100 3 4 5 100

直接通過 陣列名[下標] 就可以對其進行引用賦值,如果下標不存在,自動新增新乙個陣列元素

刪除:[chengmo@centos5 ~]$ a=(1 2 3 4 5)

[chengmo@centos5 ~]$ unset a

[chengmo@centos5 ~]$ echo $

[chengmo@centos5 ~]$ a=(1 2 3 4 5)

[chengmo@centos5 ~]$ unset a[1]

[chengmo@centos5 ~]$ echo $

1 3 4 5

[chengmo@centos5 ~]$ echo $ 4

直接通過:unset 陣列[下標] 可以清除相應的元素,不帶下標,清除整個資料。

linux shell 陣列建立及使用技巧

1.陣列定義 chengmo centos5 a 1 2 3 4 5 chengmo centos5 echo a 1一對括號表示是陣列,陣列元素用 空格 符號分割開。2.陣列讀取與賦值 chengmo centos5 echo 5用 可以得到陣列長度 chengmo centos5 echo 3 ...

linux shell 陣列建立及使用技巧

linux shell在程式設計方面比windows 批處理強大太多,無論是在迴圈 運算。已經資料型別方面都是不能比較的。下面是個人在使用時候,對它在陣列方面一些操作進行的總結。1.陣列定義 chengmo centos5 a 1 2 3 4 5 chengmo centos5 echo a 1一對...

linux shell 陣列建立及使用技巧

linux shell在程式設計方面比windows 批處理強大太多,無論是在迴圈 運算。已經資料型別方面都是不能比較的。下面是個人在使用時候,對它在陣列方面一些操作進行的總結。1.陣列定義 chengmo centos5 a 1 2 3 4 5 chengmo centos5 echo a 1一對...