bash array

bash 仅支持一维数组。

并且数组下标是从0開始的
为数组赋值:
array=(1 4 7 2 5 8)     #以空格为切割符,()为数组
str="this is test string"
str_arr=($str);         #默认以空格切割
数组遍历:
for val in str_arr[*];do echo $val; done
for file in `ls`;do echo $file; done
数组元素个数: ${#array}

举例: 将一个字符串的每一个字符切割为数组。


hjj@ThinkPad:~$ str="long string"
hjj@ThinkPad:~$ for((i=0;i<${#str};i++))do a[i]=${str:$i:1}; echo ${a[i]}; done;
l
o
n
g

s
t
r
i
n
g     

原文地址:https://www.cnblogs.com/claireyuancy/p/6906748.html