Double quotes in the shell, single quotes, back quotes

Source: Internet
Author: User
Tags echo command


There are three types of quotes in the shell: single quotes, double quotes, and anti-quotes. The single quotation mark ' 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. Single quotation marks are used to hold the literal value of all characters within the quotation marks, even if the \ and carriage returns within the quotation marks are no exception, but single quotes cannot appear in the string. (Note that it is all, except that the single quote itself cannot appear in it).
For example:
$ string=’$PATH’
$ echo $string
$PATH
Visible $ maintains its own meaning, appearing as ordinary characters. Double quotation marks are used to keep the literal value of all characters in quotation marks (carriage return is no exception), except in the following cases:
$ plus variable name to take the value of the variable
Anti-quotation marks still indicate command substitution
\$ represents the literal value of $
The literal value of \ ' representation '
The literal value of \ "represents"
\ \ denotes the literal value of \
In addition to the above, there is no special meaning in front of other characters, only the literal value.
For example, we assume that the value of path is.:/ Usr/bin:/bin, enter the following command:
$ TestString=”$PATH\$PATH”
$ echo $TestString
.:/usr/bin:/ bin$PATH
The reader can try it out on its own before the second double quotation mark does not add any results. Anti-quote ' anti-quote (') The key for this character is usually located in the upper-left corner of the keyboard, and do not confuse it with single quotation marks ('). 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. For example:
$ pwd
/home/xyz
$ string=”current directory is `pwd`”
$ echo $string
current directour is /home/xyz


When the shell executes the echo command, it first executes the command pwd in ' pwd ' and outputs the result/home/xyz instead of ' pwd ', and outputs the entire result after the replacement.



This function of anti-quote can be used for command substitution, that is, the execution result of the inverted quotation marks is assigned to the specified variable. Of course, the anti-quote can also be replaced with $ () , and the anti-quotation marks are older usage. For example:


$ today=`date`
$ echo Today is $today
Today is Mon Apr 15 16:20:13 CST 1999


Anti-quotes can also be nested. It is important to note, however, that the inner layer's back quotation marks 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 5
$ 


Special characters from 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
$



Double quotes in the shell, single quotes, back quotes


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.