๐ง Parameter Expansion
Parameter expansion is one of the most powerful features of POSIX shells.
It enables variable substitution, default values, substring extraction, pattern removal, and error handling โ all without invoking external tools.
๐ Who This Is For
- Engineers writing productionโgrade shell scripts
- DevOps/SRE teams optimizing automation performance
- Developers replacing external tools (
sed, cut, awk) with builtโin expansions
- Anyone needing predictable, portable string manipulation in POSIX shells
๐งฉ Role in the Ecosystem
Parameter expansion is a core part of the POSIX shell execution model.
It provides:
- Fast, builtโin string manipulation
- Portable alternatives to external commands
- Error handling for required variables
- Safe defaults for unset or empty parameters
- A foundation for writing robust scripts
It is supported by all POSIX shells (dash, ash, ksh, bash in POSIX mode).
๐งฉ Key Concepts
1. Default and Fallback Values
| echo "${VAR:-fallback}" # use fallback if VAR is unset or empty
echo "${VAR:=default}" # assign default if VAR is unset or empty
|
2. Required Variables
| : "${TOKEN:?TOKEN is required}"
|
| name="robert"
echo "${name:0:3}" # rob
echo "${name:2}" # bert
|
4. String Length
5. Pattern Removal
| path="/var/log/nginx/access.log"
echo "${path%/*}" # /var/log/nginx
echo "${path##*/}" # access.log
|
6. Indirection (bash/ksh)
| var="TARGET"
TARGET="value"
echo "${!var}" # value
|
๐ง Notable Techniques
Prefix/Suffix Removal
| file="backup_2025.tar.gz"
echo "${file#backup_}" # 2025.tar.gz
echo "${file%.gz}" # backup_2025.tar
|
PatternโBased Replacement (bash/ksh)
| text="a b c"
echo "${text// /_}" # a_b_c
|
Safe Variable Expansion
| echo "${VAR:-}" # expands to empty string if unset
|
Numeric Expansion (bash/ksh)
| num=5
echo "$(( num + 1 ))" # 6
|
โ ๏ธ Limitations & Pitfalls
- Some expansions are not POSIX (e.g.,
${var//pattern/repl})
- Dash/ash do not support substring syntax
${var:offset:length}
- Indirection
${!var} is bash/kshโonly
- Pattern matching uses shell globs, not regex
- Quoting rules can change expansion behavior
- Expansion inside double quotes preserves whitespace; unquoted does not
๐ง When to Use Parameter Expansion
- Replacing external tools for performance
- Writing portable
/bin/sh scripts
- Validating required variables
- Manipulating paths, filenames, and strings
- Ensuring predictable behavior in CI/CD pipelines
โ When Not to Use Parameter Expansion
- When complex parsing is required (use
awk or a real language)
- When portability across nonโPOSIX shells is uncertain
- When readability suffers due to overly dense expansions
โ
Best Practices
- Prefer parameter expansion over external tools for simple operations
- Use
${var:?message} for required variables
- Use
${var:-} to avoid unbound variable errors
- Quote expansions unless intentional:
- Avoid bashโonly expansions in
/bin/sh scripts
- Test expansions under multiple shells (bash, dash, ash, ksh)
๐งช Testing Parameter Expansion
| set -x
VAR=""
echo "${VAR:-fallback}"
echo "${VAR:=default}"
echo "${#VAR}"
|
Test portability:
| dash script.sh
bash --posix script.sh
ksh script.sh
|
๐ง Summary
Parameter expansion is a fast, portable, and powerful mechanism for manipulating variables in POSIX shells.
It replaces many external tools, improves performance, and enables robust error handling โ making it essential for productionโgrade scripting.