Linux Command Reference ยท Bash Syntax
Bash parameter expansion
Reference for Bash parameter expansion: default values, error on unset, length, trimming prefixes and suffixes, search and replace, indirection and arrays.
Parameter expansion is everything that happens when Bash sees ${...}. Beyond simply reading a variable, it can supply defaults, fail when a variable is missing, measure length, cut off prefixes and suffixes, and replace text, all without starting another program.
Using it instead of sed, basename or cut in scripts is faster and avoids fork overhead. Case changes and slicing are covered on the Substring and case expansion page.
${name:-default} # use a default when unset or empty ${name%.txt} # remove a suffix ${name//a/b} # replace every a with b
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
${parameter:-word} ${parameter-word} ${parameter:=word} ${parameter=word} ${parameter:?word} ${parameter?word} ${parameter:+word} ${parameter+word} ${#parameter} ${parameter#pattern} ${parameter##pattern} ${parameter%pattern} ${parameter%%pattern} ${parameter/pattern/string} ${parameter//pattern/string} ${parameter/#pattern/string} ${parameter/%pattern/string} ${!name} ${!prefix*} ${!array[@]}
| Form | Meaning |
|---|---|
:-word |
Use word if the parameter is unset or empty. Without the colon, only if unset. |
:=word |
Same, but also assign word to the parameter. Not allowed for positional parameters. |
:?word |
Print word as an error and exit a non-interactive shell if unset or empty. |
:+word |
Use word only if the parameter is set and non-empty; otherwise nothing. |
#param |
Length in characters (number of elements for ${#arr[@]}). |
#pat, ##pat |
Remove the shortest / longest match of pat from the start. |
%pat, %%pat |
Remove the shortest / longest match of pat from the end. |
/pat/str |
Replace the first match. // replaces all matches. /# and /% anchor to the start or end. An empty str deletes the match. |
!name |
Indirection: expand the variable whose name is stored in name. |
!prefix*, !prefix@ |
Names of variables beginning with prefix. |
!arr[@] |
The indexes (or keys) of an array. |
Returns: the expanded string. Edge cases: the patterns are shell globs (*, ?, [...]), never regular expressions; quote the pattern to match special characters literally.
Usage notes
- The colon matters.
${v:-x}treats empty like unset;${v-x}only replaces a truly unset variable. Pick deliberately when empty is valid input. - Mnemonic for trimming:
#is on the left of$on a keyboard, so it removes from the left;%is on the right, so it removes from the right. Doubling the sign means the longest match. - Replace
basenameanddirnamewith${path##*/}and${path%/*}. Edge cases (trailing slashes, no slash) differ, so keepbasenamewhen input is untrusted. - A
:=default executes the assignment in the current shell, but not inside a subshell or command substitution, where the change is lost. - Use
: "${VAR:?message}"at the top of a script to require configuration. The:command does nothing but forces the expansion. - Replacement text may contain
&in newer Bash versions only whenpatsub_replacementis enabled (Bash 5.2), so do not rely on&meaning the match as insed. - Operating on arrays applies the operation to every element:
"${arr[@]/a/A}". Quote the whole expansion to keep elements intact. ${!ref}works for simple indirection; for more control use a nameref (Namerefs).- Unset variables with
set -uraise an error unless a default is given, so${v:-}is the safe way to read an optional value under strict mode.