Você pode fazer isso usando indireção. Na% man_de% manpage:
If the first character of parameter is an exclamation point (
!
), it introduces a level of variable indirection. Bash uses the value of the variable formed from the rest of parameter as the name of the variable; this variable is then expanded and that value is used in the rest of the substitution, rather than the value of parameter itself. This is known as indirect expansion. The exceptions to this are the expansions of${!prefix*}
and${!name[@]}
described below. The exclamation point must immediately follow the left brace in order to introduce indirection.
Para o seu exemplo:
#!/bin/bash
AR=('foo' 'bar' 'baz' 'bat')
for i in "${!AR[@]};" do
printf '${AR[%s]}=%s\n' "$i" "${AR[i]}"
done
Isso resulta em:
${AR[0]}=foo
${AR[1]}=bar
${AR[2]}=baz
${AR[3]}=bat
Observe que isso também funciona para índices não-sucessivos:
#!/bin/bash
AR=([3]='foo' [5]='bar' [25]='baz' [7]='bat')
for i in "${!AR[@]};" do
printf '${AR[%s]}=%s\n' "$i" "${AR[i]}"
done
Isso resulta em:
${AR[3]}=foo
${AR[5]}=bar
${AR[7]}=bat
${AR[25]}=baz