Parameter expansion

Beginner · 6 min

Use ${var} forms to supply defaults, trim prefixes and suffixes, replace text, change case and measure length without calling sed, cut or basename.

Before thisVariables and the environment

Script Bit · 19 min · from 4:38

Parameter expansion

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Script BitReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • `${var}` is the full form of `$var`. The braces let you attach operators that transform the value before it is used.
  • `${var:-default}` substitutes a fallback, `${var:=default}` also assigns it, and `${var:?message}` aborts a script when the variable is empty.
  • `${var#pattern}` and `${var%pattern}` trim from the front and back. Doubling the symbol makes the match greedy.
  • `${var/old/new}` replaces the first match and `${var//old/new}` replaces all of them. `${#var}` gives the length.
  • `${var^^}`, `${var,,}` and `${var@Q}` need Bash 4 or 5. macOS's `/bin/bash` 3.2 rejects them as a bad substitution.

The problem it solves

Scripts spend a surprising amount of time doing small surgery on strings: strip the directory from a path, drop an extension, supply a default when an argument is missing. The classic move is to launch basename, sed or cut and capture the output. That works, but each call starts a whole new process to shave a few characters.

Parameter expansion does these jobs inside the shell. The syntax is ${...}, and the characters after the variable name are the operation. Think of the braces as a small pipe of one: the value goes in, a transformed value comes out, and the variable itself is untouched.

Defaults and guards

A script that takes an optional argument needs a fallback:

$ unset name; echo "hello ${name:-stranger}"; echo "name is still [${name-unset}]"
hello stranger
name is still [unset]
$ echo "hello ${name:=stranger}"; echo "name is now $name"
hello stranger
name is now stranger

:- substitutes the default without touching the variable. := substitutes and also assigns, which is why name changed. The colon matters:

$ log=; echo "[${log-fallback}] [${log:-fallback}]"
[] [fallback]

Without the colon the fallback applies only when the variable is unset. With it, an empty variable counts too. For arguments and config, you nearly always want the colon form.

${var:?message} is a guard. If var is unset or empty, Bash prints the message and stops a non-interactive shell:

$ echo "${missing:?need a value for missing}"; echo "not reached?"
bash: missing: need a value for missing

The second echo never ran. That makes it a one-line argument check.

Trimming from the front and back

# removes a matching prefix and % removes a matching suffix. The pattern is a glob, the same kind from the globbing lesson, not a regular expression. One symbol takes the shortest match, two take the longest. A way to remember it: # sits on the left of $ on a US keyboard, and % sits on the right.

$ f=/var/log/nginx/access.log.gz
$ echo "${f##*/}"
access.log.gz
$ echo "${f%/*}"
/var/log/nginx
$ echo "${f%.gz}"
/var/log/nginx/access.log
$ echo "${f%%.*}"
/var/log/nginx/access
$ echo "${f##*.}"
gz

Read ${f##*/} as “remove the longest prefix ending in a slash”, which leaves the file name. ${f%/*} removes the shortest suffix starting with a slash, which leaves the directory. Those two are basename and dirname with no extra process. The ${f%%.*} result is a warning as much as a technique. It removes everything from the first dot in the whole string, so on /srv/app.d/access.log.gz it leaves /srv/app (I ran that), chopping inside the directory name. Trim the file name on its own, after ${f##*/}, when directories might contain dots.

Length, slices and replacement

$ echo "${#f}"
28
$ echo "${f:5:3}"
log
$ echo "${f/nginx/apache}"
/var/log/apache/access.log.gz
$ echo "${f//g/G}"
/var/loG/nGinx/access.loG.Gz

${#f} is the length. ${f:5:3} starts at offset 5 and takes three characters. One slash replaces the first match and two replace every match. The pattern is again a glob.

Case and quoting operators need a modern Bash

$ word=Hello; echo "${word^^} ${word,,}"
HELLO hello
$ echo "${word@Q}"
'Hello'

^^ upper-cases, ,, lower-cases, and @Q prints the value quoted so it could be pasted back in as input. All three arrived in Bash 4 or later. Here is what stock macOS does:

$ /bin/bash -c 'word=Hello; echo "${word^^}"'
/bin/bash: ${word^^}: bad substitution

I got the same bad substitution for ${word,,} and ${word@Q} on 3.2.57. If a script has to run on a Mac’s default shell, use tr for case. Everything earlier in this lesson, including trimming, replacement and defaults, worked on 3.2 as well. The %, %%, ## and // forms ran there too, as did :=.

For positional arguments, ${@:2} expands to everything from the second one on (two three in my test) and ${#@} counts them.

About the video

Script Bit’s “Bash Tutorial Ep. 2 - Shell Expansions, Arithmetic” is 18 minutes 35 seconds and was published in April 2026. I could not read a transcript, but the description has chapters, and “parameter and variable expansion” runs from 4:38 to 10:30, so the embed plays exactly that six-minute stretch (278 to 630 seconds). I have not checked which operators those six minutes use, so the @ operators and the Bash 3.2 failures above come from my own runs. It is in English. I first wanted You Suck at Programming’s longer parameter-expansion deep dive, but it has embedding switched off, so it can’t play here. Two shorter YSAP videos on the basics and on the @ operators are linked as second views, judged by title.

Where this fits

Parameter expansion transforms a value you already hold. The next lesson generates values: brace and tilde expansion.

Check yourself

  1. 01What is the difference between `${x-fallback}` and `${x:-fallback}`?

    Without the colon, the fallback applies only when `x` is unset. With the colon, it also applies when `x` is set to an empty string.

  2. 02Given `f=/var/log/nginx/access.log.gz`, what do `${f##*/}` and `${f%/*}` produce?

    `access.log.gz` and `/var/log/nginx`. They do the same job as `basename` and `dirname` without starting a process.

  3. 03How do `%` and `%%` differ?

    Both remove a matching suffix. `%` removes the shortest match and `%%` removes the longest, so with `a.tar.gz`, `${f%.*}` leaves `a.tar` and `${f%%.*}` leaves `a`.

  4. 04You write `echo "${word^^}"` and it works on your laptop but fails on a stock Mac. Why?

    Case modification arrived in Bash 4, and `/bin/bash` on macOS is 3.2. The script needs a newer Bash or a different tool such as `tr`.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark