Special symbols and their escape
As you know, a shell command is made up of a command name and its arguments, for examplecat testfile, where cat is the command name and Testfile is a parameter. The shell passes the parameter testfile to the cat command. However, if the parameter contains special characters, such as *, * we know, is any number (including 0) of any character that is represented. So the way the shell works is that it will pre-process these special characters and then pass the results of the processing to that command.
For example, if your current directory has file, File1, file2 three files, then when you execute cat file*, the shell will pre-process the parameter part file*, because * represents any number (including 0) any character, then the result of processing is file File1 file2, the shell then passes the result, file File1 file2, to the cat command, so you see the same result as directly executing the cat file File1 file2, which is that the contents of the three files are displayed at the command-line terminal.
In fact, we have a way to see what the shell is dealing with, or what the actual command is like, and that is to add echo before the command you want to execute.
For example, in the above directory you executeecho cat file*, then you will see
?
The command above is the one that is actually executed after the shell has processed the parameters.
But a lot of the time, we want to pass to these commands, is the parameters themselves, for example, your current directory has a file name is called test*, while there is another file, test1. And you just want to see the content of test*, this time, if you perform
, then the contents of test* and Test1 will be displayed. This involves the escape of special characters. The so-called escape, meaning is to remove its special meaning. Let the shell treat them as normal characters, without special handling.
In the shell, there are three ways to escape special characters
- Use \ escape: put \ Characters in front of special characters
- Escape with single quotation mark ('): Enclose the argument in single quotation marks
- Escape with double quotation marks (""): Enclose the argument in double quotation marks
In the above example, these three methods are efficient, that is to say, the following three commands can achieve the purpose:
- cat test\*
- cat ‘test*‘
- cat “test*”
Range of three escape methods
In the shell, the special characters are very many, and the above mentioned three kinds of escape methods are not the same range, where the first method is the widest range, almost all special characters can be escaped, the common use is to put \ At the end of a command line, Such a long command can be divided into two or more lines of writing. Like what
123 |
echothis is a \very long \line |
will be displayed as
1 |
this is a very long line |
Here, in fact, is to escape the newline character, so that it loses the meaning of a newline, while preventing the command to execute immediately.
The second type of escape (single quotation mark) is smaller than the first, which prevents the command line from executing at once, but it does not allow the newline character to lose the meaning of line breaks. So
will be displayed as
The third type of escape (double quotation marks) is smaller than the second, and single quotes can Escape "'", while double quotes do not. In the shell, something in the middle of "'" is treated as a command substitution. When the shell pre-processes the parameters, it executes the two "'" objects in the middle as a shell command, and then replaces the execution results with their own. As an example:
will display
1 |
today is Mon Oct 12 16:14:16 CST 2015 |
Of course, the exact date is related to the period in which you executed the order. In short, you should understand the meaning. Back to the original question, we say that single quotes can Escape "'", and double quotes do not, meaning that the "'" between the single quotes will lose its special meaning, and the "'" between the double quotes will still have a special meaning, so
?
will display
?
and
?
will display
?
1 |
today is Mon Oct 12 16:23:23 CST 2015 |
Some of the common special characters
- *Any of any characters
- ?An arbitrary character
- [..] []Any one of the characters, here is also similar to the regular expression, in parentheses can be specific characters, such as [ABCD] can also be used-specify a range, such as[a-d]
- #Comments
- (space) Parameter delimiter
- cmdCommand substitution
- |Pipeline
- &Background execution
- ;Command delimiter (two commands can be executed on the same line; split)
- ~User home Directory
Finally remember, at any time, you are not sure whether you are escaped right, or the special word trailing characters, remember to add in front of the commandecho, to see the actual execution of the command is how
Detailed escape of special characters in shell command line (special meanings removed)