7.2 Quoting


7.2 Quoting

A literal is a string that you want the shell to leave alone and pass to the command line. To see where a literal is useful, consider how the shell normally expands * to all files and directories in the current directory, then passes all those items on to the current command line. If you just want a star ( * ) to be used by a command, as you would for grep and other programs that use regular expressions, you need special notation to pass a literal * .

The easiest way to make the shell leave a string alone is to enclose the entire string in single quotes. Here's an example with grep and * :

 grep 'r.*t' /etc/passwd 

All characters located between the two single quotes, including any spaces, make up a single literal. Therefore, the following command does not work, because it asks the grep command to search for the string r.*t /etc/passwd from the standard input (because there is only one parameter):

 grep 'r.*t /etc/passwd' 

Double quotes ( " ) work just like single quotes, except that the shell expands any variables that appear within double quotes, whereas it does not expand variables located between single quotes. You can see the difference by running the following command and then replacing the double quotes with single quotes and running it again.

 echo "There is no * in my path: $PATH" 

One tricky part to using literals with the Bourne shell is passing a literal single quote to a command. One way to do this is to place a backslash before the single quote character:

 echo I don\'t like contractions inside shell scripts. 

The backslash and quote must appear outside any pair of single quotes. Astring such as 'don\'t results in a syntax error. However, oddly, you can enclose the single quote inside double quotes, as the following example shows (the output is identical to that of the preceding command):

 echo "I don't like contractions inside shell scripts." 



How Linux Works
How Linux Works: What Every Superuser Should Know
ISBN: 1593270356
EAN: 2147483647
Year: 2004
Pages: 189
Authors: Brian Ward

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net