Linux下數組遍歷

聲明一個數組變量數組

直接賦值:spa

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

declare聲明:.net

declare -a array

小括號空格法:code

array=(Zero One Two)

引號空格法:htm

array=」Zero One Two」

混合法:blog

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

遍歷一個數組get

標準for循環:for循環

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

for…in:class

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

相關文章
相關標籤/搜索