The difference between double quotes, single quotes, and anti-quotes in shell scripts

Source: Internet
Author: User
Original: http://blog.csdn.net/iamlaosong/article/details/54728393

Recently, to compile a shell script to process the data, you need to detect the existence of the data file, the file name contains the date, so you need to generate the date of the last few days, in order to detect the existence of the file, see how the following script in the use of double quotes, single quotes, anti-quotes:

[plain] view plain copy opdate= ' date-d '-1 day ' +%y%m%d ' for I in $ (seq.) do filedate= ' date-d '-$ I day "+%y%m%d ' echo ${filedate} done
1. The character nonalphanumeric, enclosed by single quotes, appears as a normal character. When special characters are enclosed in single quotes, they lose their original meaning and are interpreted only as ordinary characters. such as '-1 day ' in the routine, for example:

$ String= ' $PATH '
$ echo $string
$PATH
$
Visible $ maintains its own meaning, appearing as ordinary characters.

2. Characters enclosed in double quotation marks, except $ (dollar sign), \ (backslash), ' (single quotation mark), and ' (double quotation marks) are still special characters and retain their special functions, remaining characters are treated as ordinary characters. In the case of "$", the variable and $ are replaced by the value of the variable specified later, and for "\" It is an escape character that tells the shell not to treat the character that follows it specially, just as a normal character. As you can see, in double quotes you need to precede the words "\" with only four characters $,\, ' and ' itself. The "number", if not preceded by "\", then the shell will match it with the previous "number. The "-$i Day" in the routine converts the $i to its represented value as a loop.

3, the inverse quotation mark (') the corresponding key is usually located in the upper left corner of the keyboard, do not confuse it with the single quotation mark ('). The string that is enclosed in quotation marks is interpreted by the shell as the command line, and at execution time, the shell executes the command line first and replaces the entire anti-quotation mark (including two anti-quote) portions with its standard output results. As in the routine ' date-d '-1 day ' +%y%m%d ' is to assign the result of this command to the variable opdate.

4, anti-quotation marks can also be nested use. It is important to note, however, that the inner anti-quote must be escaped with a backslash (\) when nested. For example:
$ Abc= ' echo the number of users is \ ' who| Wc-l\ "
$ echo $ABC
The number of users is 2
$
5. Special characters of the shell can also be used in the command line between the anti-quotes. The shell is actually going to execute the specified command in order to get the result of the command in. When executed, the special characters in the command, such as $, ",? And so on, will have a special meaning, and ' can contain any valid shell command, such as:
$ ls
Note Readme.txt Notice Unix.dir
$ teststring= "' echo $HOME ' ls [nn]* ']
$ echo $TestString
/HOME/YXZ Note Notice

Finally, say the digression: The anti-quote is an old usage, and $ () is the new usage, such as $ (seq 10) in the routine. $ () is the recommended usage both in learning and in real work. so the above usage can be changed to:

$ teststring= "$ (echo $HOME) $ (LS [nn]*)]

$ echo $TestString

/HOME/YXZ Note Notice

For the execution of the script, you can use the following command to analyze:

Sh-x test.sh


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.