Single and Double Quoting in a POSIX Shell

Quoting controls how the shell interprets characters before constructing an argument. Different quoting forms permit different expansions.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Compare one variable reference
  2. Check the language of invocation
  3. Compare expansion inside two quoted fields
  4. Check the grammar of the actual shell

Compare one variable reference

If a variable name holds blue, quoting "$name" normally inserts that value as part of one argument. Single-quoted '$name' passes the literal dollar sign and name. Shell argument structure describes where those values appear.

Check the language of invocation

A terminal may run a different shell with different details. Preserve an exact example and expected argument list when reproducing a run rather than copying quoting rules across environments.[1]

Compare expansion inside two quoted fields

In a POSIX shell with name=River, printing the argument '$name' passes a literal dollar sign and letters. With "$name" the shell expands the variable and normally preserves the result as one field. A value containing spaces still forms one argument in the double-quoted case. See program argument vectors.

input                       observed argument
'$name'                     $name
"$name" with name=River     River
"$name" with name=Blue Bay  Blue Bay, one argument

Check the grammar of the actual shell

PowerShell and other shells have distinct quoting details. Test what the called program receives if reproducing between shells. If launching from code, a structured list often avoids having to reassemble shell quoting.

References

  1. The Open Group, POSIX Shell Command Language ↩