Sobes.tech
Middle

What does the $@ construct mean in bash?

sobes.tech AI

Answer from AI

In bash, the $@ construct represents a list of all positional parameters (arguments) passed to a script or function, with each argument preserved as a separate element.

The difference between $@ and $* is how they are handled when used within quotes:

  • "$@" — each argument is treated as a separate string, which is convenient for passing arguments further without losing separation.
  • "$*" — all arguments are combined into a single string, separated by the first character of the IFS variable (usually a space).

Example:

#!/bin/bash
for arg in "$@"; do
  echo "Argument: $arg"
done

If the script is called with arguments one "two three" four, the output will be:

Argument: one
Argument: two three
Argument: four

This is important for correct handling of arguments with spaces.