bash how to echo array


You can define three elements array (there are no space between name of array variable, equal symbol and starting bracket):

FILES=(report.jpg status.txt scan.jpg)

This command will write each element in array:

echo ${FILES[*]}

Index in shell arrays starts from 0. So, if you want to write just first element, you can do this command:

echo ${FILES[0]}

Output: report.jpg

Do you want to process each emelent in array in loop? Here is an example:

for ELEMENT in ${FILES[@]}
echo File: $ELEMENT.

If you want to get only indexes of array, try this example:

echo ${!FILES[@]}

"${!FILES[@]}" is relative new bash's feature, it was not included in the original array implementation.

Was this information helpful to you? You have the power to keep it alive.
Each donated € will be spent on running and expanding this page about UNIX Shell.