🎓 A1Learner
🔍
0 of 50 lessons completed

Shell Quoting

Quoting controls how the shell reads words. Single quotes keep text literal, and double quotes let variables expand.

Single quotes stay literal

In single quotes, $name stays as the plain text $name.

name=Ana
echo '$name'

Double quotes expand

In double quotes, $name expands to Ana.

name=Ana
echo "$name"

No quotes split words

Without quotes, the shell splits the words and echo joins them with one space.

text="Buy   milk"
echo $text

Double quotes keep spaces

In double quotes, the three spaces stay as they are.

text="Buy   milk"
echo "$text"

No quotes expand globs

Without quotes, *.jpg expands to the three photo names.

echo photos/*.jpg

Double quotes stop globs

In double quotes, *.jpg stays as plain text.

echo "photos/*.jpg"

The same in zsh

zsh follows the same rule: double quotes let $name expand.

name=Ana
echo "$name"

Example

text="Buy   milk"
echo $text
echo "$text"
echo '$text'

Key points

  • Single quotes: the shell reads everything inside as plain text.
  • Double quotes: variables inside still expand to their values.
  • No quotes: the shell splits words and expands globs.
  • A glob: a pattern like *.jpg that matches file names.

In this example, the first echo has no quotes, so the shell splits the words and prints one space. The second echo has double quotes, so the three spaces stay. The third echo has single quotes, so $text stays as plain text.

Note

zsh follows the same three quoting rules as bash.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

What do single quotes do?