Linux Variable Expansion and Parameter Substitution
Variable expansion and parameter substitution are fundamental
mechanisms in Linux shell environments that allow scripts to dynamically
evaluate, manipulate, and inject values before executing commands. When
a shell like Bash or Dash encounters a dollar sign ($), it
initiates an expansion phase that replaces variable names with their
stored values or applies inline transformation logic. This article
breaks down how the Linux shell parses these expressions, handles
conditional fallback values, executes pattern-based string
manipulations, and fits substitution into its broader command execution
lifecycle.
The Mechanism of Variable Expansion
In Linux shell scripting, a parameter is an entity that stores
values, which can be a name (variable), a number (positional parameter
like $1), or a special symbol (such as $? or
$$). Variable expansion is the process of retrieving the
value assigned to that parameter.
The most basic form uses the $ prefix:
NAME="Linux"
echo $NAMEWhen unambiguous separation is required between the variable name and adjacent characters, curly braces are mandatory:
PREFIX="super"
echo "${PREFIX}user" # Outputs: superuserWithout the braces, the shell attempts to evaluate
$PREFIXuser, which likely returns an empty string if
undefined.
Parameter Substitution Operations
Parameter substitution extends basic expansion by allowing
conditional checks, default value assignments, and string manipulation
directly within the ${} syntax, eliminating the need for
external tools like awk or sed.
1. Conditional and Fallback Expansions
Shells provide conditional operators to handle unset or null variables:
- Default Value (
${VAR:-default}): UsesdefaultifVARis unset or null. The original variable remains unchanged. - Assign Default (
${VAR:=default}): IfVARis unset or null, it setsVARtodefaultand expands to that value. - Display Error (
${VAR:?error_message}): Throws an error witherror_messageand terminates the script ifVARis unset or null. - Alternate Value (
${VAR:+alternate}): Expands toalternateonly ifVARis set and not null; expands to nothing otherwise.
2. String Length and Slicing
The shell can compute string length or extract substrings natively:
- String Length (
${#VAR}): Returns the number of characters in the value ofVAR. - Substring Extraction
(
${VAR:offset:length}): Extracts a slice starting at the zero-based indexoffsetfor a specifiedlength. Iflengthis omitted, it extracts to the end of the string.
TEXT="OperatingSystem"
echo "${TEXT:0:9}" # Outputs: Operating3. Pattern Removal (Trimming)
Shell parameter substitution allows prefix and suffix truncation using standard glob patterns:
- Remove Shortest Prefix
(
${VAR#pattern}): Deletes the shortest match ofpatternfrom the beginning ofVAR. - Remove Longest Prefix
(
${VAR##pattern}): Deletes the longest match ofpatternfrom the beginning ofVAR. Commonly used to get file basenames. - Remove Shortest Suffix
(
${VAR%pattern}): Deletes the shortest match ofpatternfrom the end ofVAR. Commonly used to strip file extensions. - Remove Longest Suffix
(
${VAR%%pattern}): Deletes the longest match ofpatternfrom the end ofVAR.
FILE="/path/to/archive.tar.gz"
echo "${FILE##*/}" # Outputs: archive.tar.gz
echo "${FILE%.*}" # Outputs: /path/to/archive.tar4. Pattern Replacement
Bash and compatible shells support search-and-replace transformations:
- Replace First Match
(
${VAR/pattern/replacement}): Replaces the first match ofpatternwithreplacement. - Replace All Matches
(
${VAR//pattern/replacement}): Replaces every match ofpatternwithreplacement. - Anchor Replacement (
${VAR/#pattern/replacement}or${VAR/%pattern/replacement}): Restricts the substitution to the prefix (#) or suffix (%) of the string.
Order of Shell Expansion
The Linux shell does not evaluate expressions at random. It follows a strict, sequential order of operations before passing arguments to a command:
- Brace Expansion: Expressions like
a{b,c}expand toab ac. - Tilde Expansion:
~expands to the user's home directory. - Parameter and Variable Expansion:
${VAR}substitutions occur. - Command Substitution: Expressions like
$(command)or`command`are executed, and their output replaces the expression. - Arithmetic Expansion: Expressions inside
$(( ... ))are computed. - Word Splitting: Unquoted expansions are scanned
according to the Internal Field Separator (
IFS, usually space, tab, newline) and split into distinct arguments. - Pathname Expansion (Globbing): Patterns like
*or?are replaced with matching filenames. - Quote Removal: Single and double quotation marks used to suppress previous expansions are removed.
Because word splitting occurs immediately after parameter expansion,
failing to wrap variables in double quotes ("$VAR") can
cause variables containing whitespace to split into multiple distinct
arguments, often resulting in scripting bugs or security
vulnerabilities. Double-quoting preserves the integrity of the expanded
text as a single word.