Detailed escape of special characters in shell command line (special meanings removed)

Source: Internet
Author: User



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


?
1 catfilefile1, file2


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


1 cattest*


, 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


    1. Use \ escape: put \ Characters in front of special characters
    2. Escape with single quotation mark ('): Enclose the argument in single quotation marks
    3. 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:


    1. cat test\*
    2. cat ‘test*‘
    3. 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


12 echo‘helloworld‘


will be displayed as


12 helloworld


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:


1 echotoday is `date`


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


?
1 echo‘today is `date`‘


will display


?
1 today is `date`


and


?
1 echo“today is `date`”


will display


?
1 today is Mon Oct 12 16:23:23 CST 2015


Some of the common special characters


    1. *Any of any characters
    2. ?An arbitrary character
    3. [..] []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]
    4. #Comments
    5. (space) Parameter delimiter
    6. cmdCommand substitution
    7. |Pipeline
    8. &Background execution
    9. ;Command delimiter (two commands can be executed on the same line; split)
    10. ~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)


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.