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.

The @ symbol in Bash is a special parameter and array subscript that expands to all positional parameters or all elements of an array. Its primary mechanical function is to handle multi-word arguments and array elements, treating each expanded element as a discrete, individual word when evaluated in contexts subject to word splitting.

Positional Parameters Expansion

When used as a standalone special parameter, @ references the arguments passed to the current shell or function. Its behavior is strictly dictated by whether it is enclosed in double quotes and the evaluation context.
$@
"$@"
  • Unquoted ($@): Expands to all positional parameters starting from $1. The shell subsequently subjects the expanded result to word splitting (based on IFS) and pathname expansion (globbing).
  • Double-quoted ("$@"): In contexts where word splitting is performed (such as command arguments), it expands each positional parameter into a separate, distinct word, bypassing word splitting and pathname expansion. Mechanically, it is equivalent to "$1" "$2" ... "$N". However, in contexts where word splitting is not performed (such as scalar variable assignments like var="$@" or inside [[ ]] conditional tests), "$@" does not expand into separate words. Instead, it flattens the elements into a single string separated by a space, regardless of the value of IFS.

Array Element Expansion

When applied as a subscript within curly braces, @ targets the elements of an array.
${array_name[@]}
"${array_name[@]}"
  • Unquoted (${array_name[@]}): Expands all values in an indexed or associative array, subject to standard word splitting and globbing.
  • Double-quoted ("${array_name[@]}"): Expands each array element as a separate word (in word-splitting contexts), preserving the exact string boundaries of each element as they were originally assigned.

Array Key/Index Expansion

By prepending the parameter name with the ! operator inside the curly braces, @ evaluates to the indices (for indexed arrays) or keys (for associative arrays) rather than the values.
${!array_name[@]}
"${!array_name[@]}"

Element Counting

When combined with the # length operator, @ returns the total number of elements rather than the elements themselves.
${#@}              # Returns the count of positional parameters
${#array_name[@]}  # Returns the count of elements in the specified array

Parameter Slicing

The @ operator supports offset and length expansion to extract a contiguous subset of elements.
${@:offset:length}
"${array_name[@]:offset:length}"
This operation is strictly valid only for positional parameters and indexed arrays. Applying substring expansion (slicing) to an associative array produces undefined results or errors. The syntax expands to length elements starting from the specified offset. If length is omitted, it expands to all elements from the offset to the end of the array or positional parameters.

Pattern Substitution

The @ operator allows search and replace operations to be mapped across all elements of an array or all positional parameters simultaneously.
${@/pattern/replacement}
"${array_name[@]/pattern/replacement}"
The shell applies the pattern substitution to each element individually. When double-quoted in a word-splitting context, the discrete word boundaries of each modified element are preserved.

Technical Distinction: @ vs *

The @ operator is frequently contrasted with the * operator. Their mechanical differences are exposed during double-quoted expansion and when handling empty parameter lists:
"$@"  # Expands to N discrete words: "$1" "$2" "$3" (in word-splitting contexts)
"$*"  # Expands to 1 single word, delimited by the first character of IFS: "$1c$2c$3"
  • Empty Parameters: If there are zero positional parameters (or when an array is empty), "$@" expands to nothing (zero words). In contrast, "$*" expands to a single empty string (one word).
  • Delimiters and Non-Word-Splitting Contexts: In contexts where word splitting is performed, "$@" consistently maintains the original token boundaries and discrete element separation regardless of the IFS state. In contexts where word splitting is not performed, "$@" flattens elements using a space as the delimiter, whereas "$*" flattens elements using the first character of IFS.
Master Bash with Deep Grasping Methodology!Learn More