Bash: loop over arguments passed to script

July 6, 2022  ‐ 1 min read

To loop over the arguments that are passed to the bash script we can make use of the special parameters $@. This special parameter expands to the passed positional arguments starting from position one. Thus excluding the script name at position zero.

for argument in "$@"; do
  echo "$argument"

The special parameter $* could be an alternative to $@. However, $@ handles arguments containing spaces correctly.

When no arguments are passed $@ expands to nothing, leaving you with an empty string.