【发布时间】:2016-07-27 02:10:49
【问题描述】:
这是我的数组:
$ ARRAY=(one two three)
我如何打印数组,以便得到如下输出:index i, element[i] 使用下面使用的 printf 或 for 循环
1,one
2,two
3,three
一些笔记供我参考
1种方式打印数组:
$ printf "%s\n" "${ARRAY[*]}"
one two three
2种方式打印数组
$ printf "%s\n" "${ARRAY[@]}"
one
two
three
3种方式打印数组
$ for elem in "${ARRAY[@]}"; do echo "$elem"; done
one
two
three
4种方式打印数组
$ for elem in "${ARRAY[*]}"; do echo "$elem"; done
one two three
查看数组的另一种方式
$ declare -p ARRAY
declare -a ARRAY='([0]="one" [1]="two" [2]="three")'
【问题讨论】: