The difference between the anti-quote and $ () in a shell script

Source: Internet
Author: User


The difference between the anti-quote and $ ():

Let's first look at an example:

Create a file named Test.sh, enter the following two instructions in the file

650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M00/82/DB/wKioL1ditWDAq5FZAAAJ9atF8Qc539.png-wh_500x0-wm_3 -wmp_4-s_3680320313.png "title=" 1.PNG "alt=" Wkiol1ditwdaq5fzaaaj9atf8qc539.png-wh_50 "/>

The results of the operation are as follows:

650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M01/82/DC/wKiom1ditFqQf72EAAAMMSzbC7o195.png-wh_500x0-wm_3 -wmp_4-s_3539604763.png "title=" 2.PNG "alt=" Wkiom1ditfqqf72eaaammszbc7o195.png-wh_50 "/>

Why are the two statements running differently?

This is a very interesting question, but to explain the problem is quite difficult, I would like to change a simple example, a popular explanation.

For example, we have a shell script named test1.sh with two lines

650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M00/82/DC/wKiom1ditG_yzqAvAAALnqJppRU204.png-wh_500x0-wm_3 -wmp_4-s_3476569554.png "title=" 3.PNG "alt=" Wkiom1ditg_yzqavaaalnqjppru204.png-wh_50 "/>

We can use Sh-x test1.sh to analyze how bash interprets this script, and the information returned is as follows:

650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M00/82/DB/wKioL1ditaDyUJqQAAAW1KMq5vg285.png-wh_500x0-wm_3 -wmp_4-s_2155111376.png "title=" 4.PNG "alt=" Wkiol1ditadyujqqaaaw1kmq5vg285.png-wh_50 "/>

We can draw two conclusions:

1. \$ does not convert the special meaning of $ in the inverted quotation mark contains the contents of ECHO \ $hostname is still interpreted as an echo $HOSTNAME takes the value of this variable and outputs so the value returned by the inverse quotation mark is Localdomain

$ () on the contrary, $ is obviously escaped to a normal character, so it does not take the value of the variable, but returns the meaning of the string itself, and returns the $hostname

2. This indicates that the inverted quotation marks have a special conversion to the backslash (\), and at least we see that when we use 1 \ directly, it does not itself escape. Now let's make some changes to the contents of the script:

650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M01/82/DC/wKiom1ditJqDv6tYAAALWaveLlA588.png-wh_500x0-wm_3 -wmp_4-s_3819798427.png "title=" 5.PNG "alt=" Wkiom1ditjqdv6tyaaalwavella588.png-wh_50 "/>

We've added 1 more backslashes, this time we'll look at the output:

650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M02/82/DB/wKioL1ditdDBxPldAAAnkLM7jqM457.png-wh_500x0-wm_3 -wmp_4-s_2408731810.png "title=" 6.PNG "alt=" Wkiol1ditddbxpldaaanklm7jqm457.png-wh_50 "/>

This time it looks like a reversal:

1. Counter-quotes instead output the literal meaning of $hostname

2.$ () as we wish to take the value of the variable, and rightfully output an escaped string \

Let's do another experiment and build a a.sh with a foot name:

650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M00/82/DC/wKiom1ditNTDS7U4AAAIG_97TGk595.png-wh_500x0-wm_3 -wmp_4-s_679179395.png "title=" 7.PNG "alt=" Wkiom1ditntds7u4aaaig_97tgk595.png-wh_50 "/>

Note: The last inverted quotation mark and) are preceded by a space, otherwise the last anti-quote and) will be commented, and will not be used as the ending substitution symbol.

The results of using sh-x a.sh are as follows:

650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M01/82/DB/wKioL1ditg_Th5MBAAAYBkNQGxM929.png-wh_500x0-wm_3 -wmp_4-s_3690905200.png "title=" 8.PNG "alt=" Wkiol1ditg_th5mbaaaybknqgxm929.png-wh_50 "/>

From what we can see:

1.4 \ is output as 1 \ In the inverted quotation mark

2. $ () 4 \ are output to 2 \

We have modified the script to 8 \:

650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M00/82/DB/wKioL1ditiqhiQgTAAAKagCrwLw120.png-wh_500x0-wm_3 -wmp_4-s_2318970954.png "title=" 9.PNG "alt=" Wkiol1ditiqhiqgtaaakagcrwlw120.png-wh_50 "/>

Then look at the output:

650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M01/82/DC/wKiom1ditSXCsVgCAAAeaCmLzkc875.png-wh_500x0-wm_3 -wmp_4-s_1304316199.png "title=" 10.PNG "alt=" Wkiom1ditsxcsvgcaaaeacmlzkc875.png-wh_50 "/>

Thus, we can conclude that:

1. The anti-quotation mark itself is escaped by itself, preserving the meaning of the alignment itself, if we want to play the special meaning in the anti-quotation marks, we must use 2 \ to represent.

2. So we can simply imagine the inverted quotation mark: \ \ = \

3. $ () does not need to consider the problem, as we normally use: \ = \

Note: Anti-quotes are old usage, $ () is a new usage, whether in the learning test, or in the actual work, the usage of $ () is recommended.


The difference between the anti-quote and $ () in a shell script

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.