[英]Accessing array index variable from bash shell script loop?
I want to access the array index variable while looping thru an array in my bash shell script. 我想在bash shell脚本中循环访问数组时访问数组索引变量。
0
1
2
3
Actual result
foo bar baz bat
Desired result
0 1 2 3
How do I alter my script to achieve this? 如何更改脚本以实现此目的?
You can loop over index using indirect reference syntax ( since Bash 3 ) : 您可以使用间接引用语法遍历索引( 自Bash 3起 ):
#!/bin/bash
A=('foo' 'bar' 'baz' 'bat')
for i in ${!A[*]}; do # replace ${A[*]} with ${!A[*]}
echo $i
done
For more : How to iterate over associative arrays in Bash 有关更多信息: 如何在Bash中迭代关联数组
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.