Quoted characters do not have a special meaningA surprising number of characters have special meanings. The lowly space, often forgotten in many books, is an extremely important meta-character. Consider the following:
- rm -i file1 file2
Can a file have a space in the name? Absolutely. This is UNIX. There are few limitations in filenames. As far as the operating system is concerned, You can't have a filename contain a slash or a null. The shell is a different story, and one I don't plan to discuss.
Normally, a space delineates arguments. To include a space in a filename, you must quote it. Another verb used in the UNIX documentations is "escape;" this typically refers to a single character. You "quote" a string, but "escape" a meta-character. In both cases, all special characters are treated as regular characters.
Assume, for a moment, you had a file named "file1 file2," This is one file, with a space between the "1" and the "f." If this file is to be deleted, one way to quote the space is
- rm 'file1 file2'
- rm 'file1 file2' rm file1' 'file2 rm f'ile1 file'2
There are two other forms or quoting. The second uses a backslash "," which only acts to "escape" the next character. The double quotation mark is similar to the single quotes used above, but weaker. I'll explain strong and weak quotation later on. Here is the earlier example, this time using the other forms of quoting:
- rm "file1 file2" rm file1 file2 rm file1" "file2
Nested quotations
A very confusing problem is placing quotation marks within quotation marks. It can be done, but it is not always consistent or logical. Quoting a double quote is perhaps the simplist, and does what you expect:- echo '"' echo """ echo "
- echo '' echo "" echo
As you can see, single quotes and double quotes behave differently. A double quote is weaker, and does not quote a backslash. Single quotes are different again. You can escape them with a backslash, or quote them with double quotes:
- echo ' echo "'"
- echo '''
It is identical to
- echo '
Strong versus weak quoting
Earlier I described single quotes as strong quoting, and double quotes as weak quoting. What is the difference? Strong quoting prevents characters from having special meanings, so if you put a character inside single quotes, what you see is what you get. Therefore, if you are not sure if a character is a special character or not, use strong quotation marks.Weak quotation marks treat most characters as plain characters, but allow certain characters (or rather meta-characters) to have a special meaning. As the earlier example illustrates, the backslash within double quotation marks is a special meta-character. It indicates the next character is not, so it can be used before a backslash and before a double quotation mark, escaping the special meaning. There are two other meta-characters that are allowed inside double quotation marks: the dollar sign, and the back quote.
Dollar signs indicate a variable. One important variable is "HOME" which specifies your home, or starting directory. The following examples illustrates the difference:
- $ echo '$HOME' $HOME $ echo '$HOME' $HOME $ echo "$HOME" /home/barnett $ echo "$HOME" $HOME
- $ echo 'The current directory is `pwd`' The current directory is `pwd` $ echo 'The current directory is `pwd`' The current directory is `pwd` $ echo "The current directory is `pwd`" The current directory is `/home/barnett` $ echo "The current directory is `pwd`" The current directory is `pwd`
Quoting over several lines
There is a large difference between the C shell and the Bourne shell when a quote is larger than a line. The C shell is best suited for interactive sessions. Because of this, it assumes a quote ends with the end of a line, if a second quoute character is not found. The Bourne shell makes no assumptions, and only stops quoting when you specify a second quotation mark. If you are using this shell interactively, and type a quotation mark, the normal prompt changes, indicating you are inside a quote. This confused me the first time it happened. The following Bourne shell example illustrates this:- $ echo 'Don't do this' > ls > pwd > ' Dont do this ls pwd $
#!/bin/sh # Print a warning if any disk is more # than 95% full. /usr/ucb/df | tr -d '%' | awk ' # only look at lines where the first field contains a "/" $1 ~ /\// { if ($5 > 95) { printf("Warning, disk %s is %4.2f%% full\n",$6,$5); } }'
Click here to get file: diskwarn.sh
Mixing quotation marks
Having two types of quotation marks simplifies many problems, as long as you remember how meta-characters behave. You will find that the easiest way to escape a quotation mark is to use the other form of quotation marks.- echo "Don't forget!" echo 'Warning! Missing keyword: "end"'
Quotes within quotes - take two
Earlier I showed how to include a quote within quotes of the same kind. As you recall, you cannot place a single quote within a string terminated by single quotes. The easiest solution is to use the other type of quotation marks. But there are times when this is not possible. There is a way to do this, but it is not obvious to many people, especially those with a lot of experience in computer languages. Most languages, you see, use special characters at the beginning and end of the string, and has an escape to insert special characters in the middle of the string. The quotation marks in the Bourne shell are not used to define a string. There are used to disable or enable interpretation of meta-characters. You should understand the following are equivalent:- echo abcd echo 'abcd' echo ab'c'd echo a"b"cd echo 'a'"b"'c'"d"
- 'string1'"string2"'string3'
- $ echo 'Strong quotes use '"'"' and weak quotes use "' Strong quotes use ' and weak quotes use "
Placing variables within strings
Change the quoting mid-stream is also very useful when you are inserting a variable in the middle of a string. You could use weak quotes:- echo "My home directory is $HOME, and my account is $USER"
- echo 'My home directory is '$HOME', and my account is '$USER
No comments:
Post a Comment