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?

Prism Glasses

Aching back from coding all day? Try Back Problems

Ever feel achy from sitting crunched up on your computer table? Try lying down with these optical glasses that allow you to work on your laptop while lying flat on your back. This is the perfect solution with those with limited mobility or those who wish to prevent neck cramps and back strains.

$ Check price
4.454.45Amazon 4 logo(128+ reviews)

More Back Problems resources

Take your Linux skills to the next level!

System Admin Handbook

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

This book approaches system administration in a practical way and is an invaluable reference for both new administrators and experienced professionals. It details best practices for every facet of system administration, including storage management, network design and administration, email, web hosting, scripting, and much more.

$ Check price
74.9974.99Amazon 4.5 logo(142+ reviews)

More Linux & UNIX resources

Ad