Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

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.
$0       # The name of the calling script or shell (special parameter, not strictly positional)
$1       # The first positional parameter
$2       # The second positional parameter
...
$9       # The ninth positional parameter
${10}    # The tenth positional parameter (braces are mandatory)
${11}    # The eleventh positional parameter

Aggregate Special Parameters

Bash provides specific special parameters to evaluate or expand the entire array of positional parameters at once:
$#       # Expands to the decimal count of positional parameters currently set.
$*       # Expands to all positional parameters.
$@       # Expands to all positional parameters.

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.

# Initial state: $1="alpha", $2="beta", $3="gamma", $#=3
shift 1

# Resulting state: $1="beta", $2="gamma", $#=2
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.

# Clears existing parameters and assigns new ones
set -- "new_arg1" "new_arg2" "new_arg3"


# Resulting state: $1="new_arg1", $2="new_arg2", $3="new_arg3", $#=3
Master Bash with Deep Grasping Methodology!Learn More