home ~ projects ~ socials

Create And Loop Through An Array Of Strings In Bash

If all the values you're working with are single words you can process them as a string. See: 2so4kki3

This is how to create an array

NAMES=("alfa bravo" "charlie delta" "echo foxtrot")

for name in "${NAMES[@]}"; do
  echo $name
done
Output:
alfa bravo
charlie delta
echo foxtrot

Notes

  • Don't put commas between items in the array or they'll show up in the output. (i.e. don't do "alfa bravo", "charlie delta")
  • The quotes around `${NAMES[@]}bash are necessary to keep the strings from splitting on spaces
-- end of line --

References