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

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

${var:-default} ${var:?msg} ${#var} ${var:off:len} ${var#pat} ${var%pat} ${var/a/b} ${var^^}

Parameters

ParameterDescription
${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

Path manipulation
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.

Defaults, replace, case, substring
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.

Was this page helpful?