Linux下数组遍历

声明一个数组变量

直接赋值:

array[0]=”Zero” array[1]=”One” array[2]=”Two”

declare声明:

declare -a array

小括号空格法:

array=(Zero One Two)

引号空格法:

array=”Zero One Two”

混合法:

array=([0]=”Zero” [1]=”One” [2]=”Two”) 

遍历一个数组

标准for循环:

for(( i=0;i<${#array[@]};i++)) 
do
  echo ${array[i]}
done
Zero
One
Two

for…in:

for i in ${array[@]}
do
  echo $i
done
Zero
One
Two

获取数组的长度

echo ${#array[@]}
3
echo ${#array[*]}
3

删除数组元素

unset array[0]
echo ${#array[@]}
2
for i in ${array[@]}
do
  echo $i
done One Two

切片

echo ${array[@]:0:1}
Zero
echo ${array[@]:0:2}
Zero One
echo ${array[@]:0:3}
Zero One Two
echo ${array[@]:1}
One Two
echo ${array[@]:2}
Two

 

参考:

http://www.jb51.net/article/55253.htm(以上内容转自此篇文章)

http://blog.csdn.net/tutuboke/article/details/48200839

http://www.jb51.net/article/120792.htm

http://blog.csdn.net/redhat456/article/details/6068409

http://blog.csdn.net/xujingzhong0077/article/details/53667279

posted @ 2018-01-18 08:23  EasonJim  阅读(11413)  评论(1编辑  收藏  举报