Shell Array
shell支持一维数组(不支持多维数组),并且没有限定数组的大小。
Defining arrays
shell中,用括号来表示数组,数组元素用空格分隔,下标从0开始
Mode 1
数组名=(值1 值2...值n),如array_name=(value0 value1 value2)
Mode 2
array_name=(value0 value1 value2 )
Method 3 defines the value of the array individually
array_name[0]=value0array_name[1]=value1array_name[2]=value2
Reading an array
获取数组某个元素的值${数组名[下标]} 使用@可以获取数组中的所有元素${数组名[@]}
Gets the length of the array
获取数组长度的方法和获取字符串长度的方法相同。length=${#数组名[@]}length=${#数组名[*]}获取数组某元素的长度length=${#数组名[下标]}
#shell#!/bin/shlist1=("east" "west" "north" "south")list2=(‘one‘ ‘two‘ ‘three‘ ‘four‘)list3[0]="陆风"list3[1]="江铃"list3[3]="长安"echo "list1: "${list1[@]} ${#list1[@]}echo "list2: "${list2[@]} ${#list2[@]}echo "list3: "${list3[@]} ${#list3[@]}echo "list3[0]: "${list3[0]} ${#list3[0]}echo "list3[2]: "${list3[2]} ${#list3[2]}
List1:east West North South 4
List2:one three four 4
List3: Land Breeze Jiangling Chang ' an 3
List3[0]: Land Breeze 2
LIST3[2]: 0
Shell 3 Array