码迷,mamicode.com
首页 > 编程语言 > 详细

Bash的数组

时间:2015-09-20 23:56:12      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:

 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

Bash的数组

标签:

原文地址:http://www.cnblogs.com/pandachen/p/4824595.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!