← Back to Bash Course | Chapter 2: Variables & Types | Lesson 3 of 13

Quoting Rules

Quotes tell the computer how carefully to read your words. Double quotes let it swap in the values of your labels, while single quotes say 'read this exactly as written, no swapping'.

In this page:

  1. Quoting Rules
Syntax
bash
echo "double quotes expand: $variable"
echo 'single quotes are literal: $variable'

Quoting Rules

Double quotes ("...") allow variable expansion and command substitution inside them, while single quotes ('...') treat everything literally, including $.

Unquoted variables get word-split and glob-expanded by the shell, which can cause bugs with spaces in values. Quoting variables consistently is one of the most important Bash habits.

Note: Always quote variables you expand, like "$name", unless you specifically want word-splitting.

Example: Quoting Rules

bash
#!/bin/bash
name="Bob Smith"

echo "Double quotes: $name"
echo 'Single quotes: $name'
echo Unquoted: $name
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using single quotes and expecting variable expansion, since '$name' prints $name literally.
  2. Leaving variables unquoted, so a value with spaces is split into several words.
  3. Nesting the same kind of quotes without escaping, which ends the string early.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.