The difference between single and double quotes in a Linux shell (there is no question of quotation marks after reading) (GO)

Source: Internet
Author: User
Tags echo command

The difference between "" (double quotation marks) and "(single quote)
 
You hit the keyboard behind the shell prompt, and until you press ENTER, the text you typed is command line, and the shell executes the command you submitted in a process manner. But what do you know about each word you type in command line, and what kind of category does it have for the shell?

In simple terms, each charactor of command line is divided into the following two types:
*literal: That is plain text, there is no special function for the shell.
*meta: For the shell, a reserved word with a specific function.
Literal there is nothing to say, generally ABCD, 123456 and other such "words" are literal. But Meta does often confuse us. In fact, in the first two chapters we have encountered two meta-encounters in command line almost every time:
*ifs: Consists of <space> <tab> <enter> One of the three (we use space).
*CR: Generated by <enter>.
IFS is used to split the command line with every word (word), because shell command line is handled by word. And CR is used to end command line, which is why we knock <enter> command to execute. In addition to IFS and CR, the commonly used meta is:
=: Sets the variable.
$: Do a variable or operation substitution (please do not confuse the shell prompt).
>: redirect stdout.
<: redirect StdIn.
|: Pipeline command.
&: Redirect The file descriptor or place the command in the background.
(): Place commands inside the nested Subshell execution, or for operations or command substitution.
{}: Place the command inside the non-named function or use it in the defined scope of the variable substitution.
; : Proceed to the next command at the end of the previous command, ignoring its return value.
&&: At the end of the previous command, if the return value is true, proceed to the next command.
|| : At the end of the previous command, if the return value is false, proceed to the next command.
!: Execute the commands in the history list
....
If we were to close the function of the reserved metacharacters in command line, we would have to use the quoting processing.
In bash, there are three ways we use the quoting:
*hard Quote: "(single quote), where all meta-quote are closed.
*soft Quote: "" (double quotes), most of the meta in soft quote will be closed, but some reservations (such as $).
*escape:\ (backslash), only a single meta is closed immediately after the escape (caret).

The following examples will help us understand the quoting:

$ a=b C # blank key was not closed as IFS processing.
$ c:command not found.
$ echo $A

$ a= "B C" # Blank key has been closed, only whitespace symbol processing.
$ echo $A
B C

When setting the A variable for the first time, command line will be interpreted as if the blank key is not closed:
* A=b then touches <ifs&gt, and then executes the C command.
The second time a variable is set, because the blank key is placed in soft quote, it is closed and is no longer used as IFS:
* A=b<space>c
In fact, the blank keys are closed either in soft quote or in hard quote. The Enter key is the same:
$ A= ' B
> C
> '
$ echo "$A"
B
C

In the example above, because <enter> is placed in the hard quote, it is no longer processed as a CR character.
The <enter> here is just a line break symbol (new-line), because command line does not have the CR character,
So entering the second shell prompt (PS2, denoted by the > symbol), command line will not end,
Until the third line, the <enter> we entered was not in the hard quote, so it was not closed,
At this point, command line encounters the CR character, so it ends and gives it to the shell for processing.

If the <enter> of the above example is placed in soft quote, the CR will also be closed:
$ a= "B
> C
> "
$ echo $A
B C

However, because the Echo $A variable is not placed in soft quote, the,<enter> is interpreted as IFS instead of the New line character when the variable substitution is complete and the command line is reorganized.

Similarly, use escape to close the CR character:
$ a=b\
> c\
>
$ echo $A
Bc

In the example above, the first <enter> followed by the second <enter> are closed by the escape character and therefore not treated as a CR,
But the third <enter> because it was not jumped off, so as CR ended command line.
However, because the <enter> key itself is unique in shell meta, after \ Skipping, it simply cancels its CR function and does not retain its IFS functionality.

You may find that the characters generated by a <enter> key are likely to be as follows:
CR
Ifs
NL (New line)
FF (Form Feed)
Null
...


As for the soft quote and the hard quote, the main is the closure of certain meta-or not, to the $ to explain:
$ a=b\ C
$ echo "$A"
B C
$ Echo ' $A '
$A

In the first echo command line, $ is placed in soft quote and will not be closed, so continue to handle variable substitution,
So echo outputs the variable value of a to the screen, and it gets the result of "B C".
In the second echo command line, $ is placed in hard quote, then it is closed, so $ is just a $ sign,
is not used for variable substitution processing, so the result is the $ sign followed by a letter A: $A.

--------------------------------------
Practice and thinking: Why are the results different?
$ a=b\ C
$ Echo ' "$A" ' # The outermost is the single quotation mark
"$A"
$ echo "' $A '" # The outermost is the double quotation mark
' B C '

--------------------------------------

In the shell version of the CU, I found that there are a lot of beginner's questions that are related to quoting understanding.
For example, if we call some of the previously set variables in the command arguments of awk or sed, we often ask why we can't.
To solve these problems, the key point is:
* Distinguish between shell Meta and command meta

The meta, which we mentioned earlier, has a special purpose in command line,
Let's say {} Executes a series of command line in an unnamed program (which can be simply considered command block).
However, awk needs to use {} to differentiate the command section of awk (BEGIN, MAIN, END).
If you enter this in command line:
$ awk {print $} 1.txt #这里 as explained by Shell

Because {} is not closed in the shell, the shell treats {print $} as command block,
But at the same time there is no "; "Symbol as a command compartment, so there is a syntax error with awk.

To resolve, you can use hard quote:

$ Awk ' {print $} ' 1.txt #而这里因为有 ' effect, so the $ A is actually explained by the AWK program

The hard quote above should be understood, that is, the original {, <space>, $ (note three),} these several shell meta close,
Avoid being processed in the shell, and complete the command meta in the awk parameter.
(Note Three: The $ is in the awk field number, not the awk variable,
Awk's own variables do not need to use $. )
If you understand the function of hard quote, it is not difficult to understand soft quote and escape:

awk "{print \$0}" 1.txt
awk \{print\ \$0\} 1.txt

However, if you want to change the value of Awk's $0, read it from another shell variable?
For example: If the value of the existing variable $A is 0, how do you resolve the awk $ $A in command line?
You can directly negate the hard QUOE scheme:

$ Awk ' {print $ $A} ' 1.txt

That's because $A $ cannot replace variables in hard quote.

Smart Readers (like you!), after studying in this chapter, I think, should be able to explain why we can use the following actions:

A=0
awk "{print \$ $A}" 1.txt
awk \{print\ \$ $A \} 1.txt
The combination of awk ' {print $ ' $A '} ' 1.txt #此处注意 ', the front ' combination, the back of ', the same as
awk ' {print $ ' "$A" '} ' 1.txt # Note: the "$A" package is in soft quote, where it is also important to note the combination of ' and '


Both single and double quotes can turn off the shell's handling of special characters. The difference is that double quotation marks do not have single quotation marks, single quotes close all characters with special effects, and double quotes only require the shell to ignore most, specifically, ① dollar sign ② ③ Backslash, which 3 special characters are not ignored. Not ignoring the dollar sign means that the shell also replaces the variable name inside the double quotation marks.

Here's a simple shell program to illustrate.

debian:~/learn/shell# Cat Phonebook

Alice Chebba 973-555-2015

Barbara Swingle 201-555-9257

Liz Stachiw 212-555-2298

Susan Goldberg 201-555-7776

Susan Topple 212-555-4932

Tony Iannino 973-555-1295

Stromboli Pizza 973-555-9478

debian:~/learn/shell#

debian:~/learn/shell# Cat Lu

# look someone on the phone book

grep "$" phonebook

debian:~/learn/shell#

This is the correct LU program, the following is the result of the operation.

debian:~/learn/shell#./lu ' Susan T '

Susan Topple 212-555-4932

debian:~/learn/shell#./lu Tony

Tony Iannino 973-555-1295

debian:~/learn/shell#

If Lu writes ①grep $ phonebook or ②grep ' $ ' phonebook, the following error results appear (why?). )。

Results of ①:

debian:~/learn/shell#./lu Tony//This situation turns out right

Tony Iannino 973-555-1295

debian:~/learn/shell#./lu ' Susan T '//This condition results in an error

Grep:t: No such file or directory

Phonebook:susan Goldberg 201-555-7776

Phonebook:susan Topple 212-555-4932

debian:~/learn/shell#

Results of ②:

debian:~/learn/shell#./lu Tony//This situation results in an error

debian:~/learn/shell#./lu ' Susan T '//This situation is also wrong

debian:~/learn/shell#

Turn from: http://bbs.chinaunix.net/thread-2076396-1-1.html four floor

http://blog.csdn.net/fdl19881/article/details/7849286

The difference between single and double quotes in a Linux shell (there is no question of quotation marks after reading) (GO)

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.