Skip to the workspace
Pebellslearn by building
Variables and Data  ›  Lesson

Quoting: Double, Single, None

Strings 14 minQuoting controls shell expansion. Single quotes preserve literal text; double quotes allow selected expansions while suppressing word splitting and filename expansion for ordinary parameter values.

shell bash / Quoting: Double, Single, None

Explore each scene at your own pace.

01 · The rule

The rule

Quoting: Double, Single, None

Quoting controls shell expansion. Single quotes preserve literal text; double quotes allow selected expansions while suppressing word splitting and filename expansion for ordinary parameter values.

Compare the exact arguments produced by quoted and unquoted expansions.

A student renames her folder to My Notes, runs backup.sh, and tar replies that /home/asha/My and Notes do not exist. The script did not change; the data did. Quoting is how you tell Bash where one piece of text ends, and it is the single biggest source of shell bugs.

The rule

Double quotes keep $ alive; single quotes keep everything literal.

In Bash, every value is a string. What changes is how Bash treats the text on the way into a command:

Written as Variables expand? Split on spaces? * matches files?
$src yes yes yes
"$src" yes no no
'$src' no no no
src="$HOME/My Notes"
echo '$src'     # prints: $src
echo "$src"     # prints: /home/asha/My Notes
ls $src         # ls receives two arguments: /home/asha/My and Notes
ls "$src"       # ls receives one argument, space included

Inside double quotes, a few characters stay special: $, backquote, and \. Write \$ for a literal dollar and \" for a literal double quote. Inside single quotes nothing is special, not even the backslash, so a single quote cannot appear inside single quotes at all. For text like It's done, use double quotes.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.