Use of single quotation marks, double quotation marks, and reverse quotation marks in shell
Source: Internet
Author: User
When using single quotes, double quotes, and reverse quotes in shell scripts, special symbols such as single quotes, double quotes, and reverse quotes are often used. They play different roles in shell, but are prone to misuse and confusion. Briefly summarize the usage and differences of the three. The use of single quotation marks, double quotation marks, and anti-quotation marks in single quotes... shell often uses special symbols such as single quotation marks, double quotation marks, and anti-quotation marks when writing shell scripts. They play different roles in shell, but are prone to misuse and confusion. Briefly summarize the usage and differences of the three. Single quotes close the use and interpretation of all special symbols in shell. that is, the content between single quotes is all used and interpreted in text by the meaning of common characters, whether it is special character $ or escape character. Example: 1 ~ $ A = 12; test = 'This is a $ a \ $ 'date ''; echo $ test2this is a $ a \ $ 'date' double quotation marks. it disables most special symbols in shell, but some are reserved, such as $, escape character \ (excluding \ n, \ t, etc.). It is a string of double quotation marks and does not have any function. Example: 01 ~ $ A = 12; test = "this is a $ a \ B 'date'"; echo $ test02this is a 12 \ B Thu Mar 21 15:24:45 HKT 201303 04 ~ $ A = 12; test = "'This is a $ a \ B 'date ''"; echo $ test05 'This is a 12 \ B Thu Mar 21 15:32:09 HKT 2013 '06 07 ~ $ A = 12; test = "this is a $ a \ n 'date'"; echo $ test08this is a 12 \ n Thu Mar 21 15:40:09 HKT 201309 10 ~ $ A = 12; test = "this is a $ a \ $ 'date '"; echo $ test11this is a 12 $ Thu Mar 21 15:40:38 HKT 2013 single quotation marks and double quotation marks are used to assign a string with spaces to the variable. if there is no single quotation marks or double quotation marks, shell interprets the string after the space as a command, that is, the end of the variable value assignment. 1 ~ $ A = 13; test1 = this is a $ a \ B 'date'; echo $ test12is: command not found Note: When assigning values to variables in shell scripts, no space is allowed between the variable name, equal sign, and variable value; otherwise, the above error is the same. The reverse quotation marks are used to replace commands. the strings are used for shell command execution and the execution results are returned. see the preceding example. The string enclosed in the backquotes must be an executable command; otherwise, an error occurs. Example: 1 ~ $ A = 12; test = 'This is a $ a \ B 'date'; echo $ test2No command 'others' found, did you mean: 3 Command 'thin' from package 'thin' (universe) 4 this ~ $ Echo $ (date) 2Thu Mar 21 15:54:15 HKT 2013 backslash (backslash) is generally used as an escape character. to enable the escape character, use the-e option, strings containing escape characters must use double quotation marks (1 ~ $ Echo "this is a \ n test" 2 this is a \ n test3 ~ $ Echo-e "this is a \ n test" 4 this is a5 test another function of the backslash, that is, when the backslash is used for the last character of a line, shell uses the backslash at the end of a line as the continued line. this structure is often used when several lines of long commands are input.
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.