Special Variables#

Commonly used special shell parameters and operators.

Parameters#

VariableDescription
$$PID of the current shell
$?Exit status of the last executed command (0 for success)
$#Number of positional parameters
$*All positional parameters (as a single word)
$@All positional parameters (as separate words)
$!PID of the last background command
$0Name of the script or shell (See BASH_SOURCE)
$1First positional parameter

$* vs $@#

Both $* and $@ represent all positional parameters starting from $1. They do not include $0.

The difference is significant when enclosed in double quotes:

  • "$*": Expands to a single string ("$1 $2 $3").
  • "$@": Expands to separate strings ("$1" "$2" "$3"). This is generally preferred to preserve spaces in arguments.

eg. Using "$@" in a loop:

$ for arg in "$@"; do echo "$arg"; done

Operators#

OperatorDescription
&&Logical AND (Execute next command only if previous succeeded)
`