10. Quoting and escaping "", '', \

As we have just seen, the echo has its way of interpretation depending on the syntax of its arguments. When used with plain text, however, it condenses its arguments, removing white space.

$ echo Hello          Venus    !   
Hello Venus !
$ echo Your total comes out to $1.23     
The total is .23

Additionally, you can see that it interprets anything that proceeds a dollar symbol ($) as a variable.

To manipulate which characters are interpreted or not, we can use double or single quotation marks.

Double Quotes

To have the command line echo out literal lines, with the white space intact, use double quotes.

$ echo "Hello     world"
Hello     world

Here are two more properties of double quotes:

  1. Double quotes execute everything literally except for terms following a $, \ or `.
  2. Parameter expansion and arithmetic expansion are still carried out in double quotes.

Let's try running a command that outputs the \n character, which is for a new line.

$ echo $(cal)
March 2015 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

Here, we will print out the calendar, but since all \n are ignored, we get it back as one line. To have the shell interpret the newline characters, simply use double quotes.

$ echo "$(cal)" 
March 2015 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

Single Quotes, ''

To suppress ALL expansion, use single quotes.

This is when you want to print out literal strings.

Like ~LITRALLY~ what you want.

$ echo 'Your total comes out to $1.23'
Your total comes out to $1.23

Escaping keys with \

You can also escape certain keys with the backslash (\).

$ echo "Your change is \$3.00"

Backslashes are also used as control code. Here are a few examples.

\a
bell alert
\b
backspace
\n
newline
\r
carriage return
\t
tab

Review

Now as a fun exercise, try guessing what each expansion will print:

$ echo ls *.txt hello-{a,b}-world $(echo whatsup) $((2*4))
$ echo 'ls *.txt hello-{a,b}-world $(echo whatsup) $((2*4))'
$ echo "ls *.txt hello-{a,b}-world $(echo whatsup) $((2*4))"

Aching back from coding all day?

Self-Massage Tool

Aching back from coding all day? Try Back Problems

Relieve spasms, tight muscles, trigger points and pressure points with the Body Back Buddy! This trigger point massage is designed to help you self-message any area of your body - especially those that are hard to reach. Keeping your muscles relaxes and out of contraction is importan in helping to reduce pain and prevent muscle injury.

$ Check price
29.9529.95Amazon 4.5 logo(3,443+ reviews)

More Back Problems resources

Take your Linux skills to the next level!

How Linux Works

Take your Linux skills to the next level! Try Linux & UNIX

In this completely revised second edition of the perennial best seller How Linux Works, author Brian Ward makes the concepts behind Linux internals accessible to anyone curious about the inner workings of the operating system. Inside, you'll find the kind of knowledge that normally comes from years of experience doing things the hard way.

$ Check price
39.9539.95Amazon 5 logo(114+ reviews)

More Linux & UNIX resources

Ad