Single quotation marks, double quotation marks, and reverse quotation marks in shell
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 nominal values 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.