Skip to main content
Positional parameters are a series of special, dynamically assigned variables in Bash that store the arguments passed to a script, shell, or function upon invocation. They are numerically indexed, read-only via standard assignment, and automatically populated by the shell’s parser during execution.

Syntax and Indexing

Positional parameters are referenced using digits. Single-digit parameters can be referenced directly with the $ prefix. Multi-digit parameters strictly require curly braces {} to prevent the shell from misinterpreting the variable name.

Aggregate Special Parameters

Bash provides specific special parameters to evaluate or expand the entire array of positional parameters at once:

Expansion Behavior: "$*" vs "$@"

When unquoted, $* and $@ behave identically, expanding to the list of arguments subject to standard word splitting and pathname expansion. When enclosed in double quotes, their parsing behavior diverges:
  • "$*" expands to a single string containing all parameters, separated by the first character of the IFS (Internal Field Separator) variable. It evaluates as: "$1c$2c$3" (where c is the first character of IFS). If IFS is unset, the parameters are separated by spaces. If IFS is null (e.g., IFS=), the parameters are joined without any intervening separators.
  • "$@" expands to separate strings for each positional parameter. It evaluates as: "$1" "$2" "$3". This is the standard mechanism for preserving internal whitespace and exact argument boundaries.

Parameter Manipulation

Because positional parameters cannot be reassigned using standard variable assignment syntax, Bash provides built-in commands to manipulate them. In Bash, variable names must begin with an alphabetic character or an underscore. Because a digit like 1 is not a valid identifier for assignment, the parser treats an expression like 1="value" as a command execution. This results in a command not found error (e.g., bash: 1=value: command not found), rather than a syntax error.

The shift Builtin

The shift [n] command shifts the positional parameters to the left by n positions (where n defaults to 1). The parameter at $1 is discarded, $2 becomes $1, and $# is decremented accordingly. If n is greater than the current number of positional parameters ($#), the command fails (returns a non-zero exit status) and the positional parameters remain completely unchanged.
Note: The $0 parameter is immutable and is never affected by shift.

The set Builtin

The set builtin can be used to completely overwrite the current context’s positional parameters. Using the -- delimiter ensures that subsequent strings are treated strictly as positional parameters, even if they begin with a hyphen.
Tired of Poor Bash Skills? Fix That With Deep Grasping!Learn More