Use of single quotes, double quote characters, backslashes, and anti-quotes in a Unix shell

Source: Internet
Author: User

When the shell script executes, the shell interprets the lines in the script and executes them;
For some specially processed sentences, we can use quotation marks or backslashes to prevent the shell from interpreting the execution.
As below, when you enter in the command line:
echo *
child.sh env_variable father.sh param.sh profile.sh 125017.sh
All the files under the current folder are printed by default, but we need to output a "*".
You can let the shell not interpret the asterisk (*) in this way:
echo "*"
*

Here is the shell reference type
------------------------------
"" Double quotation marks
' Single quotation mark
' Anti-quote
\ Backslash
------------------------------
1. Double quotes
Use double quotation marks to refer to any character or string except the character $, ', \.
For example:
Str= "MX2 is better than MI2"
echo "$STR"
MX2 is better than MI2
You can also remove double quotes:
Echo $STR
MX2 is better than MI2

Double quotes can cause a word with a special meaning to lose its original meaning, as follows:
Aa.
December 2012
Sun Mon Tue Wed Thu Fri Sat
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31

However, when we assign the CAL command to a variable, the following:
Calendar= "Cal"
Echo $CALENDAR
Aa.
Here, the double quotation marks are the function of the CAL command to lose the original display calendar, where the double quotation marks resemble the escape character.

Such as DB2, we want to add such a string (' 1 ', ' 2 ') to a field in the table, then how will we write it?
DB2 = + select ' 1 ', ' 2 ' from sysibm.sysdummy1
1 2
--
1 2
See from here, DB2 will ' 1 ', ' 2 ' think is 2 strings, This is because single quotes have a special meaning in the database.
We're going to escape it before we can add it to the table, which is the single quotation mark ('), as follows:
DB2 = + select ' 1 ', ' 2 ' from Sysibm.sysdummy1
sql0104n  "Select" followed by an exception tag
"1 ', ' 2". Expected tags may include: "<space>".   sqlstate=42601
Here except for the first single quote and the last single quotation mark, the rest is not escaped, so error,
The following is the correct wording:
DB2 = "1", "2" from SYSIBM.SYSDUMMY1
1
-------
' 1 ', ' 2 '

The same is true for DB2 400:
Select ' 1 ', ' 2 ' from qsys2/qsqptabl
....+.... 1 .....
Constant value
' 1 ', ' 2 '
End of Data ********

If you have double quotes in the character characters, you can use the backslash "\" to escape, as follows:
Str= "\" LENOVO "
echo "My PC is made by $STR"
My PC is made by "LENOVO
If the $ symbol is escaped, then the STR variable loses its meaning:
echo "My PC is made by \ $STR"
My PC is made by $STR

2. Single quotation marks
Single quotes are similar to double quotes, but the shell ignores any reference value, the original meaning of the special character within the masked single quotation mark.
For example: Echo ' My name is $LOGNAME ' with the result: my name is $LOGNAME

3. The difference between single and double quotation marks
Single quote ': cancels the special meaning of any character except single quotation marks.
For example, Echo ' My name is $name ' with the result: My name is $name, at which time $ is used only as a normal character.
Double quotation mark "": Cancels the special meaning of all characters except double quotation marks, $ numbers, and _ numbers
A single quotation mark is a strong reference, what is the value in the quotation mark, and what is the value of the variable;
Double quotation marks are weak references, and if the values in the quotation marks contain variables again, all of these variables are replaced immediately when the value is assigned.
For example:
echo "Who am I: $LOGNAME" results: Who am I:yeexun
echo "Who am I: ' $LOGNAME '" results: Who am I: ' Yeexun '
echo "Who am I:" $LOGNAME "results: Who am I:yeexun

Echo ' Who am I: $LOGNAME ' results: Who am I: $LOGNAME
Echo ' Who am I: ' $LOGNAME ' results: Who am I: ' $LOGNAME '
Echo ' Who am I: ' $LOGNAME ' results: Who am I:yeexun

4. Anti-Quote
The back quotation marks (') are used to set the system command output to a variable, and the shell considers the contents of the inverted quotation mark to be a system command.
So it will be executed:
Echo ' Date '
Mon Dec 16:06:55 CST 2012
This is the same as entering the date command directly:
Date
Mon Dec 16:07:12 CST 2012
If you enter a non-system command, the shell does not recognize this command and will error:
Echo ' DATE '
Ksh:DATE:not found
For the date command, refer to: http://blog.csdn.net/bobo12082119/article/details/8426831

5. Back slash
The effect of backslashes is to export some special characters as-is, these special characters are: $, *, ', +, ^, &, |, ' 、?。
Example: Echo *
Lists the file names and directory names under the current directory.
Echo \*
*
After escaping with a backslash, an asterisk is output.

Use of single quotes, double quote characters, backslashes, and anti-quotes in a Unix 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.