shell script中引号的用法
Quoting a single character with the backslash
You can prevent the shell from interpreting a character by placing a backslash ("\") in front of it. Here is a shell script that can delete any files that contain an asterisk:
echo This script removes all files that
echo contain an asterisk in the name.
echo echo Are you sure you want to remove these files\?
rm -i *\**
因为在shell中,?与*都是特殊字符,会被shell当做特殊命令来对待,其实写shell script时候特定的命令,就像在shell中执行一样。
所以这个功能同样也适用于shell自己,使用backslash("\")。
The backslash was also necessary before the question mark, which is also a shell meta-character. Without it, the shell would look for all files that match the pattern "files?." If you had the files "files1" and "files2" the script would print out
Are you sure you want to remove these files1 files2
which is not what you want.
The backslash is the "strongest" method of quotation. It works when every other method fails. If you want to place text on two or more lines for readability, but the program expects one line, you need a line continuation character. Just use the backslash as the last character on the line:
% echo This could be \ a very \ long line\! This could be a very long line! %
This escapes or quotes the end of line character, so it no longer has a special meaning. In the above example, I also put a backslash before the exclamation point. This is necessary if you are using the C shell, which treats the "!" as a special character. If you are using some other shell, it might not be necessary.
Strong Quoting with the Single Quotes
When you need to quote several character at once, you could use several backslashes:
% echo a\ \ \ \ \ \ \ b
(There are 7 spaces between 'a' and 'b'.) This is ugly but works. It is easier to use pairs of quotation marks to indicate the start and end of the characters to be quoted:
% echo 'a b'
(The HTML ruins the formatting. Imagine that there are 7 spaces between the a and b. -Bruce) Inside the single quotes, you can include almost all meta-characters:
% echo 'What the *heck* is a $ doing here???' What the *heck* is a $ doing here???
The above example uses asterisks, dollar signs, and question marks meta-characters. The single quotes should be used when you want the text left alone. If you are using the C shell, the "!" character may need a backslash before it. It depends on the characters next to it. If it is surrounded by spaces, you don't need to use a backslash.
Weak Quotes with the Double Quotes
Sometimes you want a weaker type of quoting: one that doesn't expand meta-characters like "*" or "?," but does expand variables and does command substitution. This can be done with the double quote characters:
% echo "Is your home directory $HOME?" Is your home directory /home/kreskin/u0/barnett? % echo "Your current directory is `pwd`" Your current directory is /home/kreskin/u0/barnett
Once you learn the difference between single quotes and double quotes, you will have mastered a very useful skill. It's not hard. The single quotes are stronger than the double quotes. Got it? Okay. And the backslash is the strongest of all.
Quotes within Quotes
While having two types of quotes (three if you count the backslash) might seem confusing, in reality it provides you with several ways to solve the same problems. You can put either quotes inside the other. If you want to quote single quotes, use double quotes around it. To quote double quotes, use single quotes. Heck, it's easier to show you:
% echo "Don't do that"
Don't do that
% echo 'The quote of the day is: "TGIF"'
The quote of the day is: "TGIF"
%