Previous | Table of Contents | Next |
The newline character is found at the end of each line of a UNIX shell script; it is a special character that tells the shell that it has encountered the end of the command line. You insert the newline character by pressing Enter to go to the next line when inserting text in your shell script. Normally you cant see the newline character, but if you are in the vi editor, :set list will mark each newline character with a dollar sign. You can quote the newline character to enable a long command to extend to the next line:
$ cp file1 file2 file3 file4 file5 file6 file7 \ > file8 file9 /tmp
Notice the last character in the first line is a backslash, which is quoting the newline character implied at the end of the line. The shell recognizes this and displays > (the PS2 prompt) as confirmation that you are entering a continuation line or multiple-line command.
You must not enable any spaces after the final backslash for this to work. A quoted newline is an argument separator just like a space or tab. Here is another example:
$ echo 'Line 1 > Line 2'
The newline is quoted because it is between a pair of single quotes found on two consecutive lines. Again, > is displayed by the shell and is not something you enter. Here is the output:
Line 1 Line 2
In the previous chapter, you learned that any word that contains the characters
* ? [ ]
is expanded to a list of files that match the wildcard pattern given. For example, the command
rm ch1*
removes all files whose names have the prefix of ch1. In this case, the * character is a special character. Most of the time, this is exactly what you want, but there is a case where you need to use quoting to remove the characters special meaning. Assume you have these files in a directory:
ch1 ch1* ch1a ch15
Notice that the filename ch1* contains the * character. Although this is certainly not recommended, sometimes you encounter files whose names contain strange characters (usually through some accident or mistake). If you only want to delete the file ch1*, dont do so like this:
rm ch1*
This deletes all your ch1 files. Instead, quote the special character using single quotes, double quotes, or the backslash:
rm 'ch1*'
Quoting the special character takes away its wildcard meaning and enables you to delete the desired file.
Tip:
Avoid using special characters in filenames because you have to quote the special character each time you access that file.Here again is the list of special characters:
* ? [ ] \ $ ; & ( ) | ^ < > new-line space tab
In Chapter 16, Filtering Text Using Regular Expressions, you learn about another type of wildcard called regular expression. Regular expressions use some of the same wildcard characters as filename substitution, as you can see in this grep command (which is covered in Chapter 15, Text Filters):
grep '[0-9][0-9]*$' report2 report7
The quoted string [0-9][0-9]*$ is a regular expression (wildcard) pattern that grep searches for within the contents of files report2 and report7. Wildcards in the grep pattern must be quoted to prevent the shell from erroneously replacing that pattern with a list of filenames that match the pattern.
Tip:
You should always quote your regular expressions to protect them from shell filename expansion, but sometimes they work even if you dont quote them. The shell only expands the pattern if it finds existing files whose names match the pattern. If you happen to be in a directory where no matching files are found, the pattern is left alone, and grep works fine. Move to another directory, though, and the exact same command might fail.
In Chapter 13, Input/Ouput, you see that echo enables some special characters like \n:
echo -e "Line 1\nLine 2"
This displays the following:
Line 1 Line 2
The -e option of echo enables it to interpret echo escape sequences as special, not literal, characters. Some versions of UNIX object to -e as an illegal option to echo. In that case, simply omit -e from your echo command, as it is not required on that system to enable these escape sequences.
The \n option is called an escape sequence because the preceding backslash causes the following n to be treated as a special character. How do the quoting rules apply here? If the backslash takes away the special meaning of its following character, shouldnt you just see n in the output?
Review Table 9.1. It shows that a backslash within double quotes is only special if it precedes these four characters:
\n within double quotes is treated as two normal characters that are passed to the echo command as arguments. The echo command enables its own set of special characters, which are indicated by a preceding backslash. \n passed to echo tells echo to display a newline. In this example, the \n has to be quoted so that the backslash can be passed to echo and not removed before echo can see it. Watch what happens if you dont quote the backslash:
echo Line 1\nLine 2
This displays:
Line 1nLine 2
The \n is not quoted, so the shell removed the backslash before echo sees the arguments. Because echo sees n, not \n, it simply displays n, not a newline as desired.
Previous | Table of Contents | Next |