Quoting in the Shell Print

  • 0

Getting text through unchanged.

WHY QUOTING EXISTS

To stop the shell interpreting characters you want passed literally.

WHAT DOUBLE QUOTES DO

Prevent wildcard expansion and word splitting, while still expanding variables.

WHAT SINGLE QUOTES DO

Prevent everything, including variable expansion.

WHAT A BACKSLASH DOES

Escapes the next character only.

WHAT TO USE FOR A PATH WITH SPACES

Double quotes, or escape each space.

WHY UNQUOTED VARIABLES ARE THE COMMONEST SCRIPT BUG

A value containing a space becomes two arguments, and an empty value disappears entirely.

WHAT THAT CAUSES IN A DELETION

The command acts on the wrong thing, or on everything.

WHAT THE RULE IS

Quote every variable, always, unless you have a specific reason not to.

WHAT COMMAND SUBSTITUTION IS

Running a command and using its output as text.

WHAT FORM TO PREFER

Dollar sign and parentheses, rather than backticks.

WHY

It nests, and it is far easier to read.

WHAT TO QUOTE IT WITH

Double quotes, to preserve the output intact.

WHAT TO BE CAREFUL WITH

Passwords containing special characters Filenames containing spaces, quotes or newlines

WHAT TO DO ABOUT AWKWARD FILENAMES

Handle them with find and null separators rather than parsing listings.


Was this answer helpful?
Back

Are you happy with your experience? Leave us a review on Trustpilot.


Trustpilot