Bash 5 reference · Shell Builtins and Syntax
${...} expansion (Bash syntax)
Defaults, length, substrings, prefix and suffix removal, search and replace, and case conversion on variables.
Bash 5
Syntax
${var:-default} ${var:?msg} ${#var} ${var:off:len} ${var#pat} ${var%pat} ${var/a/b} ${var^^}
Parameters
| Parameter | Description |
|---|---|
${var:-x} / ${var:=x} |
Default value / assign a default. |
${var:?msg} |
Error if unset or empty. |
${var##*/} / ${var%/*} |
Basename / directory of a path. |
${var//a/b} |
Replace all. |
${var,,} |
Lowercase. |
Examples
file=/var/log/app/access.log.gz
echo "${file##*/} | ${file%/*} | ${file%.gz} | ${#file}"
Output
access.log.gz | /var/log/app | /var/log/app/access.log | 26
Run this on your computer — see “Get Started” for setup.
unset PORT
echo "port ${PORT:-8080}"
msg='Hello World'
echo "${msg// /_} ${msg^^} ${msg:0:5}"
Output
port 8080 Hello_World HELLO WORLD Hello
Run this on your computer — see “Get Started” for setup.
Related
$(( )) arithmetic· Evaluate integer arithmetic and comparisons with $(( )) and (( )); Bash has no floating point, so use awk or bc.- Bash Parameter Expansion · tutorial