Skip to content
LumenaryAcademy
Foundations 55 min

Quoting, variables and expansions

How Bash rewrites every command line before running it, and why "quote your variables" is the most important rule in shell scripting.

Code tested · 5AI-draftedAwaiting review by a computer scientist
MotivationH

The shell rewrites your command first

In most programming languages, print(name) passes the value of name to print, and that is the end of it. In Bash, a command line goes through a series of rewrites before any command runs. Variables are replaced by their values, wildcards by lists of file names, $( ) by the output of another command, and then the result is chopped into words again. Only then does the command run, with whatever words are left.

This is what makes the shell so concise: cp *.jpg ~/backup/ copies every photo with one short line. It is also the source of most shell bugs. A variable holding a file name with a space becomes two arguments; a variable holding * turns into a list of files; an empty variable vanishes completely and shifts the remaining arguments along. Experienced shell programmers are not people who memorised more commands; they are people who can see, in their head, exactly what each line turns into before it runs.

This lesson gives you that picture: the three kinds of quoting, variables and the rich family of parameter expansions, the other expansions, the order in which they happen, and the one habit that prevents most of the trouble.

DefinitionsH

Three kinds of quoting

Quoting tells Bash which characters are data rather than syntax. There are three ordinary forms and one special one:

Definition— Single quotes

'...' preserves every character literally. Nothing expands inside, not even a backslash. The only thing a single-quoted string cannot contain is a single quote.

Definition— Double quotes

"..." preserves spaces and wildcards, but still allows $ expansions (variables, $( ), $(( ))) and backquotes. Inside double quotes, a backslash escapes only $, backquote, ", \ and newline.

Definition— Backslash

Outside quotes, \ makes the next single character literal: rm my\ file.txt names one file.

The special form is ANSI-C quoting, $'...', which works like single quotes but understands escapes such as \n (newline), \t (tab) and \' (a single quote). Here they all are; printf '[%s]\n' prints each argument it receives between brackets, so you can see exactly where the word boundaries fell:

bash
#!/usr/bin/env bash
name="Ana Lima"
printf '[%s]\n' $name                # unquoted: split into two words
printf '[%s]\n' "$name"              # double quotes: one word, $ still expands
printf '[%s]\n' '$name'              # single quotes: everything literal
printf '[%s]\n' "price: \$12, \"net\""   # backslash protects $ and " inside double quotes
printf '[%s]\n' $'one\ttab'          # ANSI-C quoting: \t becomes a real tab
printf '[%s]\n' "It's fine" 'say "hi"'
text
[Ana]
[Lima]
[Ana Lima]
[$name]
[price: $12, "net"]
[one	tab]
[It's fine]
[say "hi"]

The very first line is the whole lesson in miniature: the unquoted $name became two arguments. A good default is to use single quotes for fixed text and double quotes around anything containing a variable.

Worked examplesH

Variables and parameter expansion

A variable is set with name=value, with no spaces around =, and read with $name or ${name}. The braces are required when the name is followed by characters that could be part of it ("${prefix}_backup"), and they also unlock parameter expansion, a small language for transforming values without starting another program:

bash
#!/usr/bin/env bash
file="/srv/photos/2026/holiday.tar.gz"
echo "${file##*/}"            # remove the longest match of */ from the front
echo "${file%/*}"             # remove the shortest match of /* from the end
echo "${file%%.*}"            # remove the longest match of .* from the end
echo "${file#*.}"             # remove the shortest match of *. from the front
echo "${#file}"               # length in characters
echo "${file/photos/backup}"  # replace the first match
echo "${file//\//:}"          # replace every / with :
echo "${file:5:6}"            # substring: offset 5, length 6

city="lagos"
echo "${city^} ${city^^}"     # upper-case the first letter, or all (Bash 4.0+)

unset missing
empty=""
echo "[${missing:-default}] [${empty:-default}] [${empty-default}]"
: "${PORT:=8080}"             # assign a default if PORT is unset or empty
echo "PORT is $PORT"
text
holiday.tar.gz
/srv/photos/2026
/srv/photos/2026/holiday
tar.gz
31
/srv/backup/2026/holiday.tar.gz
:srv:photos:2026:holiday.tar.gz
photos
Lagos LAGOS
[default] [default] []
PORT is 8080

The forms worth memorising:

FormResult
${var:-word}word if var is unset or empty, else $var
${var:=word}the same, and also assigns word to var
${var:?message}stops the script with message if var is unset or empty
${var:+word}word if var is set and non-empty, else nothing
${#var}length
${var#pattern}, ${var##pattern}remove the shortest or longest match from the front
${var%pattern}, ${var%%pattern}remove the shortest or longest match from the end
${var/pat/rep}, ${var//pat/rep}replace the first, or every, match
${var:offset:length}substring
${var^}, ${var^^}, ${var,,}change case (Bash 4.0)

The patterns use the same wildcards as file names (*, ?, [abc]), not regular expressions. A mnemonic for # and %: on a US keyboard, # is to the left of $ (the front) and % is to the right (the end). Without the colon, ${var-word} tests only whether var is unset, which is why ${empty-default} above gave an empty result: empty is set, just to nothing.

The : in : "${PORT:=8080}" is a command that does nothing; it exists so that the expansion, and its side effect of assigning a default, can happen on a line of its own. This idiom is common at the top of scripts that read settings from the environment.

Variables are local to the shell unless exported: export API_URL=... makes the variable part of the environment that programs started from this shell inherit. By convention, environment variables and constants are in UPPER_CASE and a script's own variables in lower_case, which avoids accidentally overwriting important ones such as PATH, HOME or USER.

DefinitionsH

Special parameters

Some parameters are set by Bash itself. You will meet these constantly:

ParameterMeaning
$0the name of the script (or shell)
$1, $2, … ${10}positional parameters: the arguments of the script or function
$#how many positional parameters there are
"$@"all of them, each as a separate word
"$*"all of them joined into one word, separated by the first character of IFS
$?the exit status of the last command
$$the process ID of the shell
$!the process ID of the most recent background job (lesson 8)

A typical use of parameter expansion with them is a script that renames files in bulk. Trimming a suffix with % and adding a new one is safer and faster than calling sed or basename for each file, and the loop over a glob handles any file name, spaces included, as long as every expansion is quoted:

bash
#!/usr/bin/env bash
export LC_ALL=C
mkdir -p photos && cd photos
touch "beach day.jpeg" sunset.jpeg notes.txt

for f in *.jpeg; do
    mv -- "$f" "${f%.jpeg}.jpg"        # -- protects against names that start with -
done
printf '%s\n' *
text
beach day.jpg
notes.txt
sunset.jpg
ComputationC

Command substitution, arithmetic, braces and globs

Four more expansions complete the set. The script below creates a few files in its (temporary) working directory so that the wildcards have something to match:

bash
#!/usr/bin/env bash
export LC_ALL=C                         # predictable sorting for this demonstration
touch report-1.txt report-2.txt report-10.txt notes.md

echo report-*.txt                       # pathname expansion (globbing), sorted
echo report-?.txt                       # ? matches exactly one character
echo {north,south}-{1..3}               # brace expansion: no files needed
echo {08..12} {a..e..2}
echo nothing-*.csv                      # no match: the pattern is left as it is
shopt -s nullglob
echo "[" nothing-*.csv "]"              # with nullglob, no match means no words

today=$(printf '%s' "2026-11-19")       # command substitution: capture output
lines=$(printf 'a\nb\nc\n' | wc -l)
echo "on $today there were $lines lines; $(( lines * 4 + 2 )) is arithmetic"
echo "$(( 7 / 2 )) $(( 7 % 2 )) $(( 2 ** 10 )) $(( (3 + 4) * 2 ))"
text
report-1.txt report-10.txt report-2.txt
report-1.txt report-2.txt
north-1 north-2 north-3 south-1 south-2 south-3
08 09 10 11 12 a c e
nothing-*.csv
[ ]
on 2026-11-19 there were 3 lines; 14 is arithmetic
3 1 1024 14
  • Command substitution, $(command), runs a command and replaces itself with the command's output, minus trailing newlines. It nests cleanly: $(dirname "$(command -v python3)"). The old backquote form `command` does the same but nests badly; prefer $( ). Bash 5.3 added ${ command; }, which runs the command in the current shell instead of a child process, saving the cost of starting one.
  • Arithmetic expansion, $(( expression )), does integer arithmetic with C-like operators. Inside it, variables do not need $. Bash has no floating point: 7 / 2 is 3. For decimals, call awk or bc.
  • Brace expansion generates words from a pattern: {a,b} and sequences like {1..10} or {08..12} (zero-padded). It happens first, before variables are expanded, so {1..$n} does not work; use a for (( )) loop instead (lesson 4).
  • Tilde expansion turns a leading ~ into your home directory (and ~ana into Ana's).
  • Pathname expansion, or globbing: an unquoted word containing *, ? or [...] is replaced by the matching file names, sorted. If nothing matches, Bash leaves the pattern as it is, unless the nullglob option is on, in which case it disappears. Notice the sorting: report-10.txt comes before report-2.txt, because names are compared character by character, not as numbers. (LC_ALL=C at the top makes the order the same on every machine, since sorting otherwise follows the language settings.)
IntuitionH

The order of expansions

Bash performs the expansions in a fixed order, and knowing it explains nearly every puzzle:

  1. Brace expansion: {a,b}, {1..3}
  2. Tilde expansion: ~
  3. Parameter and variable expansion, arithmetic expansion and command substitution, from left to right
  4. Word splitting: the results of unquoted expansions in step 3 are split into separate words at the characters in IFS (by default space, tab and newline)
  5. Pathname expansion: words with unquoted *, ? or [ are matched against file names
  6. Quote removal: the quote characters themselves are removed

Steps 4 and 5 are the dangerous ones, and they apply only to unquoted expansions. Text you typed literally is split by the parser when it reads the line; but the value of $f or $(cmd), if left unquoted, is split again and glob-matched again. Double quotes switch both steps off. Here is what that means in practice:

bash
#!/usr/bin/env bash
touch "my notes.txt" todo.txt
f="my notes.txt"

wc -c $f 2>&1                     # unquoted: wc receives two names, "my" and "notes.txt"
wc -c "$f"                        # quoted: one name, as intended

pattern="*.txt"
echo $pattern                     # unquoted: the * is expanded as a glob
echo "$pattern"                   # quoted: literal

args=("first one" "second")
printf '<%s>' "${args[@]}"; echo  # "${array[@]}": one word per element, spaces kept
printf '<%s>' ${args[@]}; echo    # unquoted: re-split into three words
printf '<%s>' "${args[*]}"; echo  # "${array[*]}": all elements joined into one word
text
wc: my: No such file or directory
wc: notes.txt: No such file or directory
0 total
0 my notes.txt
my notes.txt todo.txt
*.txt
<first one><second>
<first><one><second>
<first one second>

The first wc looked for files called my and notes.txt (the 2>&1 merely shows its error messages in the output; lesson 3 explains it). With quotes, it counted the file we meant. The same distinction applies to the arguments of a script or function: "$@" expands to each argument as a separate word, exactly as received, and is almost always what you want; $@ and $* unquoted re-split them, and "$*" joins them into one string. Arrays, which you will use properly in lesson 5, follow the same rules with "${array[@]}".

ExplanationH

The quoting rule, and its few exceptions

The rule that professional shell programmers follow, and that ShellCheck enforces, is short:

Double-quote every expansion: "$var", "$(command)", "${array[@]}", "$@". Leave it unquoted only when you deliberately want word splitting or globbing, and then write a comment saying so.

There are a few places where quoting is unnecessary because splitting and globbing do not happen there, and you will see experienced people omit the quotes in them:

  • the right-hand side of an assignment: copy=$original is safe (but copy="$original" is equally correct, and easier than remembering the rule);
  • inside [[ ... ]] (lesson 4), except on the right of ==, where an unquoted value is treated as a pattern;
  • inside $(( ... )) arithmetic;
  • the word after case.

Why does Bash behave like this at all? Because it was designed for typing commands interactively, where splitting a variable's value into words is sometimes exactly what you want: opts="-l -a"; ls $opts. That convenience made sense in 1979 and is a liability in scripts. The modern answer for "a list of arguments in a variable" is an array: opts=(-l -a); ls "${opts[@]}", which keeps each element intact, spaces and all.

Common misconceptionsH

Common misconceptions

Misconception

"File names don't contain spaces, so quoting is pedantic." They do: downloaded files, documents from other operating systems, anything a user named. They can also contain *, newlines and leading dashes. A script that works only on "nice" names is a script waiting to delete the wrong file.

Misconception

"Single and double quotes are interchangeable, as in Python." Single quotes are completely literal; double quotes still expand $. echo 'Hello $USER' prints a dollar sign and the word USER; echo "Hello $USER" prints your user name.

Misconception

"{1..$n} loops to n." Brace expansion happens before variable expansion, so Bash sees the literal text {1..$n}, which is not a valid sequence, and leaves it as it is. Use for (( i = 1; i <= n; i++ )) or seq.

Misconception

"echo $var shows me what is in the variable." It shows the value after word splitting and globbing, and echo itself interprets some options. To see exactly what a variable holds, use declare -p var (which prints it as Bash syntax) or printf '%q\n' "$var" (which quotes any special characters).

Misconception

"Command substitution captures output exactly." It removes all trailing newlines, which is usually what you want for a single value but silently changes data that ends in blank lines. It also cannot hold the zero byte, so it is not suitable for binary data.

Misconception

"Bash can do decimal arithmetic." $(( )) is integers only, and $(( 10 / 4 )) is 2. Use awk 'BEGIN { print 10 / 4 }' (or a real programming language) when you need fractions.

Exercises

Exercises

Work out each answer on paper, step by step through the expansions, before running it.

1. Quoting, variables and expansions

Warm-upComputational

What does the second line print?

x="a    b"
echo $x

2. Quoting, variables and expansions

CoreComputational

What does the last line print?

p="/home/ana/docs/cv.final.pdf"
name=${p##*/}
echo "${name%.*} ${name%%.*} ${p%/*}"

3. Quoting, variables and expansions

CoreComputational

What does echo {1..3}{a,b} print?

4. Quoting, variables and expansions

StretchComputational

What does this print?

n=5
echo {1..$n}

5. Quoting, variables and expansions

Warm-upComputational

What does echo $(( 17 / 5 * 5 + 17 % 5 )) print?

6. Quoting, variables and expansions

StretchComputational

What does this print?

v=""
echo "[${v:-x}] [${v-y}] [${v:+z}]"

7. Quoting, variables and expansions

CoreComputational

Which commands print exactly Total: $5? Choose all that apply.

Choose all that apply

Further reading and resources