You can even do it, I just don’t know if it’s as practical as simply accessing the indexes directly, as indicated by another answer.
So let’s take parts. First we start from your array:
pessoas=(Walter Jesse Skyler Gus)
In the documentation we see that there is the syntax ${pessoas[@]}
to get the array values and ${!pessoas[@]}
to get the indexes. But if I do only echo "${pessoas[@]}"
, it prints the values on the same line. So I do:
echo "${pessoas[@]}" | tr ' ' '\n'
To swap the spaces for line breaks, so each one stays on a line. And why did I do so? To use the command paste
:
paste <(echo "${!pessoas[@]}" | tr ' ' '\n') <(echo "${pessoas[@]}" | tr ' ' '\n')
I put each command echo
(one for the indices, another for the values) within <( )
, which is called process substitution. In a nutshell, paste
takes the output of these commands, line by line, and prints the first line of each of them, then the second line of each, and so on.
The exit will be:
0 Walter
1 Jesse
2 Skyler
3 Gus
Now that I already have the indexes and their values, just read them in a loop:
pessoas=(Walter Jesse Skyler Gus)
paste <(echo "${!pessoas[@]}" | tr ' ' '\n') <(echo "${pessoas[@]}" | tr ' ' '\n') | while read indice valor
do
echo "$indice=$valor"
done
But as I said at the beginning, it’s quite a turn just to simulate the enumerate
. The solution to the other answer seems simpler to me.
Remember that this approach fails if one of the values has space. For example, if it is an array with 3 elements:
pessoas=(Walter Jesse "Gus Fring")
If I use the code above, the tr
will replace the space in the third element ("Gus Fring"), separating it into 2 lines.
To avoid this problem, we can exchange the echo
by a for
which prints the elements one per line:
paste <(for i in "${!pessoas[@]}"; do echo $i ; done) <(for i in "${pessoas[@]}"; do echo $i ; done) | while ... # o restante é igual
Ah, for the record, at
enumerate
you can set the initial value using the parameterstart
, so you don’t need to add 1 when printing: https://ideone.com/U9bDGE– hkotsubo
I didn’t know that. Thank you
– Lucas