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.

A Bash variable is a named parameter representing a memory location within the shell execution environment used to store volatile data. In POSIX shell terminology, a variable is specifically a parameter denoted by a name (an identifier consisting solely of letters, numbers, and underscores, beginning with a letter or underscore). By default, Bash variables are untyped and treated as character strings; however, context-specific operations, such as arithmetic expansion, can temporarily coerce them into integers.

Assignment Syntax

Variable assignment requires strict adherence to spacing rules. The assignment operator (=) must not be surrounded by whitespace.

# Correct syntax
IDENTIFIER="value"


# Incorrect syntax (Bash will attempt to execute IDENTIFIER as a command)
IDENTIFIER = "value"

Dereferencing and Parameter Expansion

To retrieve the value stored in a variable, the shell uses parameter expansion, triggered by the dollar sign ($). Unquoted variable expansions are subject to two critical shell operations: word splitting (where the shell divides the expanded value into multiple arguments based on the IFS variable) and filename expansion (globbing). To prevent these unintended behaviors, variable expansions should almost always be enclosed in double quotes.

# Standard expansion (passed as an argument to a command)
echo "$IDENTIFIER"


# Bracketed expansion (Strict boundary definition)
echo "${IDENTIFIER}appended_text"
Bracketed expansion is mandatory when the variable name is immediately followed by characters that could be interpreted as part of the identifier. Quoting Mechanics:
  • Double Quotes (" "): Preserves the literal value of all characters except $, `, and \. Variable expansion occurs. (Note: The exclamation mark ! is only treated as a special character if history expansion is enabled via set -H, which is disabled by default in non-interactive scripts).
  • Single Quotes (' '): Preserves the literal value of every character. Variable expansion is strictly suppressed.

Scope and Visibility

Variables in Bash operate under three primary scoping rules:
  1. Global Scope (Default): Variables declared in a script are globally accessible to the current shell environment from the point of declaration downward.
  2. Local Scope: Restricted to the execution context of a function. Declared using the local builtin.
function_name() {
    local LOCAL_VAR="scoped_value"
}
  1. Environment (Exported) Scope: Variables passed down to the environment of child processes spawned by the shell. Declared using the export builtin.
export ENV_VAR="child_accessible_value"

Type Attributes

While inherently untyped, Bash provides the declare (or typeset) builtin to apply specific attributes to variables, restricting their behavior or altering how their data is stored in memory.

# Integer attribute (forces arithmetic evaluation on assignment)
declare -i NUM_VAR=10


# Read-only attribute (immutable, cannot be unset or reassigned)
declare -r CONST_VAR="static_data"


# Indexed Array (zero-based integer indexing)
declare -a INDEXED_ARR=("val1" "val2")


# Associative Array (string-based key-value pairs, requires Bash 4.0+)
declare -A ASSOC_ARR=(["key1"]="val1" ["key2"]="val2")

Special Parameters

Bash maintains a set of internal variables that are automatically populated by the shell execution environment. These are read-only and cannot be explicitly assigned via =.
  • $0: The name of the shell or shell script.
  • $1 to $9, ${10}...: Positional parameters representing arguments passed to the script or function.
  • $#: The total count of positional parameters.
  • $@ and $*: Expands to all positional parameters. When enclosed in double quotes, their semantic behavior diverges significantly:
    • "$@" expands to individual arguments as separate words (e.g., "$1" "$2" "$3").
    • "$*" expands to a single string containing all arguments, delimited by the first character of the Internal Field Separator (IFS) (e.g., "$1c$2c$3" where c is the separator).
  • $?: The exit status of the most recently executed foreground pipeline.
  • $$: The Process ID (PID) of the current shell.
  • $!: The Process ID (PID) of the most recently executed background command.
Master Bash with Deep Grasping Methodology!Learn More