Shell脚本数组

Gáb*_*rga 9 linux arrays bash loops

我想用循环设置数组元素:

for i in 0 1 2 3 4 5 6 7 8 9
do
array[$i] = 'sg'
done

echo $array[0]
echo $array[1]
Run Code Online (Sandbox Code Playgroud)

所以它不起作用.如何..?

Oli*_*rth 7

删除空格:

array[$i]='sg'
Run Code Online (Sandbox Code Playgroud)

此外,您应该将元素作为*访问:

echo ${array[0]}
Run Code Online (Sandbox Code Playgroud)

参见例如http://tldp.org/LDP/abs/html/arrays.html.


*感谢@Mat提醒我这个!