Free Hosting Online for WorkStations

< Previous | Contents | Next >

Double Quotes

The first type of quoting we will look at is double quotes. If we place text inside double quotes, all the special characters used by the shell lose their special meaning and are treated as ordinary characters. The exceptions are “$”, “\” (backslash), and “`” (back- quote). This means that word-splitting, pathname expansion, tilde expansion, and brace expansion are suppressed, but parameter expansion, arithmetic expansion, and command substitution are still carried out. Using double quotes, we can cope with filenames con- taining embedded spaces. Say we were the unfortunate victim of a file called two words.txt. If we tried to use this on the command line, word-splitting would cause this to be treated as two separate arguments rather than the desired single argument:



[me@linuxbox ~]$ ls -l two words.txt

ls: cannot access two: No such file or directory

ls: cannot access words.txt: No such file or directory

[me@linuxbox ~]$ ls -l two words.txt

ls: cannot access two: No such file or directory

ls: cannot access words.txt: No such file or directory


By using double quotes, we stop the word-splitting and get the desired result; further, we can even repair the damage:



[me@linuxbox ~]$ ls -l "two words.txt"

-rw-rw-r-- 1 me me 18 2016-02-20 13:03 two words.txt [me@linuxbox ~]$ mv "two words.txt" two_words.txt

[me@linuxbox ~]$ ls -l "two words.txt"

-rw-rw-r-- 1 me me 18 2016-02-20 13:03 two words.txt [me@linuxbox ~]$ mv "two words.txt" two_words.txt


There! Now we don't have to keep typing those pesky double quotes.

Remember, parameter expansion, arithmetic expansion, and command substitution still take place within double quotes:


[me@linuxbox ~]$ echo "$USER $((2+2)) $(cal)"

me 4 February 2016


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


We should take a moment to look at the effect of double quotes on command substitution. First let's look a little deeper at how word splitting works. In our earlier example, we saw how word-splitting appears to remove extra spaces in our text:


[me@linuxbox ~]$ echo this is a

this is a test

test

[me@linuxbox ~]$ echo this is a

this is a test


By default, word-splitting looks for the presence of spaces, tabs, and newlines (linefeed characters) and treats them as delimiters between words. This means that unquoted spa- ces, tabs, and newlines are not considered to be part of the text. They only serve as sepa- rators. Since they separate the words into different arguments, our example command line contains a command followed by four distinct arguments. If we add double quotes:


[me@linuxbox ~]$ echo "this is a

test"

[me@linuxbox ~]$ echo "this is a


this is a test

this is a test


word-splitting is suppressed and the embedded spaces are not treated as delimiters, rather they become part of the argument. Once the double quotes are added, our command line contains a command followed by a single argument.

The fact that newlines are considered delimiters by the word-splitting mechanism causes an interesting, albeit subtle, effect on command substitution. Consider the following:


[me@linuxbox ~]$ echo $(cal)

February 2016 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

[me@linuxbox ~]$ echo "$(cal)"

February 2016


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


In the first instance, the unquoted command substitution resulted in a command line con- taining 38 arguments. In the second, a command line with one argument that includes the embedded spaces and newlines.


Top OS Cloud Computing at OnWorks: