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

Source: Internet
Author: User
Tags echo command

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


The single quotes and backquotes in shell scripts are often confused, which helps me with their different functions,

Single quotes are output as they are, and you can record them as "monotonous output". The output is as follows: the eval echo a string is monotonous:
Echo 'eval echo'

The output after the content is executed in the anti-quotation marks can be recorded as "anti-translation output". You need to reverse translate the content in the quotation marks. The following describes how to execute the eval echo a in the anti-nickname to obtain, then execute echo and finally output:
Echo 'eval echo'

In the shell language below, eval is added with single quotation marks and double quotation marks. What is the meaning of single double quotation marks?

Add eval before the command so that the shell can scan the command twice before executing the command line (first take the variable value in $ and then assign a value ).

Both single quotes and double quotes can disable shell processing for special characters. The difference is that double quotes do not have strict single quotes. Single quotes disable all characters with special functions, while double quotes only require shell to ignore the majority. Specifically, it is the dollar sign (②), the quotation mark (③), and The backslash. These three special characters are not ignored. The dollar sign is not ignored, which means shell also replaces variable names in double quotes.

Double quotation marks prevent the strings after the variable name from being considered as part of the variable name. The single quotation marks in your command do not need to be added (but the double quotation marks before = must be added ), I have tried it and it does not affect it. I should emphasize the role and use my own command-making habits.

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.