Difference between reverse quotation marks (') and $ () in Shell, shell quotation marks

Source: Internet
Author: User

Difference between reverse quotation marks (') and $ () in Shell, shell quotation marks

Someone asked today:

Echo 'echo \\\\\\ W' echo $ (echo \\\\\\\ w) Why is the output different?

This leads to another problem: is there a difference between quotation marks and $?

This is a very interesting question, but it is really difficult to explain it. Let's take a simple example and give a general explanation.

For example, we have a shell script named test. sh with two rows.

12 echo`echo \$HOSTNAME`     //Reverse quotation marksecho $(echo \$HOSTNAME)    //$()

We can use sh-x test. sh to analyze how bash interprets the script. The returned information is as follows:


We can draw two conclusions:

  1. In the back quotes, \ $ does not convert the special meaning of $ into the content contained in the back quotes. echo \ $ hostname is still interpreted as an echo $ HOSTNAME. The value of this variable is obtained and the output is reversed. the value returned by the quotation marks is the CentOS-1

  2. $ () Is the opposite. $ is obviously escaped as a common character by \, so the variable value is not obtained, but the meaning of the string is returned, so $ HOSTNAME is returned.

This indicates that there is a special conversion between the backslash (\) and the backslash (\). At least we can see that when we use one \ directly, it will not escape itself. Now let's make some changes to the script content:

12 echo`echo \\$HOSTNAME`     //Reverse quotation marksecho $(echo \\$hostname)    //$()

We added another backslash. This time we can see the output:


This time seems to have reversed:

  1. The quotation marks indicate the literal meaning of $ HOSTNAME.

  2. $ () As we wish to get the variable value, and properly output an escaped string \

Let's start an experiment and create a script named a. sh:

12 echo`echo \\\\ `    echo$(echo \\\\ )

Note: there is a space in front of the last backquotes and). Otherwise, the last backquotes and) will be commented out and will not be replaced by the ending symbol.

The result of using sh-x a. sh is as follows:


We can see that:

  1. Four \ In backquotes are output as one \

  2. 4 \ in $ () is output as 2 \

We changed the script to eight \:

12 echo`echo \\\\\\\\ `    echo$(echo \\\\\\\\ )

Let's look at the output:


Therefore, we can conclude that:

  1. The backticks themselves escape \ and retain their meanings. If we want to play the special meaning of \ In the backticks, we must use two backticks for representation.

    Therefore, we can simply think of it in the back quotes:\\= \

  2. $ () Does not need to be considered. It is the same as what we usually use:\= \

Off-the-Question: Anti-quotes are old and $ () is a new usage, whether in learning and testing or in actual work,$ () Is recommended.

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.