simonewebdesign

Loop through parameters in Bash

Iterating over the arguments in a Bash script is simpler than you might expect:

for WORD; do
  echo $WORD
done

Another way is:

for i in "$@"
do
  echo "$i"
done

In the snippet above, "$@" represents all the parameters. The echo "$i" prints each argument on a separate line.

It’s always a good idea to wrap variables within quotes, because they could contain spaces or whatever.

Last update:

View this page on GitHub