>_ IzzyLab

Hands-on task

This task is step 2 of the course “Clean scripts: shellcheck”

Quote "$@"

easy~3 min

The script /root/greet.sh should print each argument on its own line, but it breaks on arguments containing spaces. Fix the quoting so that shellcheck reports nothing and an argument like two words is treated as one.

What you'll practise:
  • shellcheck
  • quoting
  • $@
  • SC2068
  • word splitting

Topic breakdown

"$@" in double quotes is the only correct way to pass a list of arguments along: it expands each positional parameter as a separate word without splitting it again on spaces or applying glob expansion. Without quotes ($@) or with $*, bash first joins/substitutes the arguments and then splits them on IFS, so elements containing spaces fall apart. shellcheck catches this with rule SC2068.

What's checked

  1. shellcheck reports nothing
  2. an argument with a space is one argument

What it looks like