Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

When the shell processes a command line, it first parses the text into tokens ("words"), which are groups of characters separated by whitespace (one or more space characters). Quoting affects how this parsing happens, including how metacharacters are treated and how text is grouped.

There are three types of quoting in the shell:

  1. single quoting (e.g. 'some text') – this serves two purposes
    • it groups together all text inside the quotes into a single token
    • it tells the shell not to "look inside" the quotes to perform any evaluations, so
      • all metacharacters inside the single quotes are ignored
      .
      • in particular, any environment variables in the single-quoted text are not evaluated
      • no pathname globbing (e.g. *) is performed (more on globbing later...)
  2. double quoting (e.g. "some text") – also serves two purposes
    • it groups together all text inside the quotes into a single token
    • it allows environment variable evaluation, but inhibits some metacharcters
      • e.g. asterisk ( * ) pathname globbing (more on globbing later...)
      • and some other metacharacters
  3. backtick quoting (e.g. `date`)
    • evaluates the expression inside the backtick marks ( ` )
    • the standard output of the expression replaces the text inside the backtick marks ( ` )

Note that the quote characters themselves ( '  "  ` ) are metacharacters that tell the shell to "start a quoting process" then "end a quoting process" when the matching quote is found. Since they are part of the processing, the enclosing quotes are not included in the output.

Let's look at examples of these.

Single and double quotes

xx

First, to To see more on how quoting affects text grouping, we'll use quotes to define some multi-word environment variables.

Tip

Always use single ( ' ) or double ( " ) quotes when you define an environment variable whose value contains spaces.

...