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?

Foam Seat Cushion

Aching back from coding all day? Try Back Problems

This foam seat cushion relieves lowerback pain, numbness and pressure sores by promoting healthy weight distribution, posture and spine alignment. Furthermore, it reduces pressure on the tailbone and hip bones while sitting. Perfect for sitting on the computer desk for long periods of time.

$ Check price
99.9599.95Amazon 4.5 logo(9,445+ reviews)

More Back Problems resources

Take your Linux skills to the next level!

The Linux Command Line

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

The Linux Command Line takes you from your very first terminal keystrokes to writing full programs in Bash, the most popular Linux shell. Along the way you'll learn the timeless skills handed down by generations of gray-bearded, mouse-shunning gurus: file navigation, environment configuration, command chaining, pattern matching with regular expressions, and more.

$ Check price
39.9539.95Amazon 4.5 logo(274+ reviews)

More Linux & UNIX resources

Ad