Single quotation marks, double quotation marks, and reverse quotation marks in Shell

Source: Internet
Author: User
Tags echo command

In shell, there are three types of quotation marks: single quotation marks, double quotation marks, and reverse quotation marks.

Single quotation marks 'all characters enclosed by single quotation marks are common characters. When special characters are enclosed in single quotes, they will also lose their original meaning and will only be interpreted as common characters. Single quotation marks are used to keep the nominal values of all characters in the quotation marks. Even if the \ and carriage return characters in the quotation marks are no exception, the single quotation marks cannot appear in the string. (Note that it is all, but the single quotation mark itself cannot appear in it ).

For example:

$ string=’$PATH’$ echo $string$PATH$

It can be seen that $ retains its meaning and appears as a common character.

Double quotation marks

Double quotation marks are used to keep the literal value of all characters in the quotation marks (carriage return is no exception), except in the following cases:

  • $ You can add a variable name to get the value of the variable.

  • Reverse quotation marks still indicate command replacement

  • \ $ Indicates the nominal value of $

  • \ 'Indicates the literal value'

  • The nominal value of \ "represents ".

  • \ Represents the literal value \

  • In addition to the preceding conditions, the '\' before other characters has no special meaning and only indicates the nominal value.


For example, assume that the path value is.:/usr/bin:/bin and enter the following command:

$ TestString=”$PATH\”$PATH”$ echo $TestString.:/usr/bin:/ bin”$PATH$

You can try the result by yourself without adding the second double quotation mark.

 

The keys corresponding to the character 'quotation marks (') are generally located in the upper left corner of the keyboard. Do not confuse them with single quotation marks. The string enclosed by backquotes is interpreted by shell as a command line. During execution, shell first executes the command line, and replaces the entire anti-quotation mark (including two anti-quotation marks) with its standard output result. For example:

$ pwd/home/xyz$ string=”current directory is `pwd`”$ echo $stringcurrent directour is /home/xyz$


When shell executes the echo command, it first executes the command PWD in 'pwd', replaces the output result/home/XYZ with the 'pwd', and finally outputs the entire replaced result.

This function can be used to replace commands. That is, the execution result enclosed by backquotes is assigned to the specified variable. Of course, the quotation marks can also be replaced by $ (), and the quotation marks are old. For example:

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


Backquotes can also be nested. However, you must note that the inner anti-quotation marks must be escaped using a backslash () When nesting. For example:

$ abc=`echo The number of users is `who| wc-l``$ echo $abcThe number of users is 5$


Shell special characters can also be used in the command line between backquotes. Shell is used to get the result of the command in ''. It actually needs to execute the command specified in. Special characters in the command, such as $ ,",? And can contain any legal shell command, such:

$ lsnote readme.txt Notice Unix.dir$ TestString=”`echo $HOME ` ` ls 〔nN〕*`”$ echo $TestString/home/yxz note Notice$


In other cases, you can try it yourself.

Single quotation marks, double quotation marks, and reverse quotation marks in Shell

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.