Bash的数组

 Bash 2.x提供了创建一维数组的能力。

  • 有多种方法创建,用内建命令declare -a或直接数组元素赋值。
  • 向数组赋值时,如果不指定下标,下标自动从0开始,每次增加1。
  • 数组的尺寸没有限制,下标也不必是一定顺序的数字。
  • 获取数组元素的语法是:${arrayname[index]}
  • 获取所有数组元素列表的语法是:${arrayname[*]}
  • 获取数组元素数目的语法是:${#arrayname[*]}

 

$declare -a nums=(45 33 100 65)

$echo ${nums[0]}
45

$echo ${nums[*]}
45 33 100 65

$states=(ME [3]=CA [2]=CT)
$echo ${states[*]}
ME CA CT

$names=(Tom Peter Mike)

$names[5]=panda

$echo "All the array elements are ${names[*]}"
All the array elements are Tom Peter Mike panda

$echo "The number of elements in the array is ${#names[*]}"
The number of elements in the array is 4

$unset names 

=-=-=-=-=
Powered by Blogilo

posted on 2015-09-20 22:33  chenxiaopang  阅读(195)  评论(0编辑  收藏  举报

导航