用shell定義和訪問數組


定義數組

arr=(1 2 3 4 5)

訪問數組

# echo ${arr[0]}
1

打印數組中所有的值

# echo ${arr[*]}

1 2 3 4 5

打印數組長度

# echo ${#arr[*]}
5

以上定義的數組,索引只能為數字,下面將介紹關聯數組

關聯數組

首先需要用單獨的語句將變量聲明為關聯數組

# declare -A arr_array

#  arr_array=([apple]='100' [orange]='200') //定義數組 索引為apple orange

#  echo ${arr_array[apple]}         //數組指定索引的數組的值
100

獲取數組的索引列表

# echo ${!arr_array[*]}
orange apple


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM