the shell double and single quotes

Reference Links: http://bbs.chinaunix.net/thread-2076396-1-1.html

"" (Double quote) and '(single quote) difference

 
  You beat the keyboard in the back shell prompt, enter until the time of press, the text you enter is the command line, and then the shell will execute the command to process the way you submit. But, you know but also: you in every character command line input to the shell, the category of what points do?

In simple terms, each charactor command line is divided into the following two:
   * literal: that is, ordinary text, no special features to shell it.
   * meta: to shell, it has a specific function reserved words.
literal not much to say, all abcd, 123456, etc. These "words" are literal. But meta indeed often makes us confused. In fact, the first two chapters we have encountered almost every time two meta encountered in the command line:
    * IFS: a <space> <tab> <enter > one composed of three (our common space).
    * CR: produced by <enter>.
IFS is used to split the command line of each word (word) used, because the shell command line by a word processing. The CR is used to end with a command line, which is why we knocked <enter> command will be executed reasons. In addition to the IFS and CR, there are common meta:
=: set variable.
\ $: Variable or do arithmetic replaced (Please do not confuse the shell prompt).
>: Redirect stdout.
<: Redirect stdin.
|: Pipe command.
&: Redirect file descriptor, or the command in the background.
(): The command is placed within nested subshell execution, or operation, or a command substitution.
{}: Command execution function is placed within the non-named, or in the alternative variable scoping.
;: At the end of a previous command, and ignore its return value, proceed to the next command.
&&: At the end of a previous command, if the return value is true, proceed to the next command.
||: At the end of a previous command, if the return value is false, proceed to the next command.
!: Execution history list of commands
....
If we want to close these reservations metacharacters function command in the line, then we would use the quoting process.
In bash, we often quoting the following three ways:
    * hard quote: '' (single quotation marks), where all the hard quote in the meta are closed.
    * soft quote: "" (double quotation marks), most meta will be in the soft quote is closed, but some reservations (eg $).
    * escape: \ (backslash), only a single meta immediately in escape (escape character) was only after the close.


The following example will help us understand quoting the:

        \ $ A = BC # blank key is not closed, as the IFS process.
        \ $ C: Command Not found.
       \ $ Echo \ $ A
        
        \ A $ = "C B" # key blank has been closed, as only the blank symbol processing.
        \ $ Echo \ $ A
        the BC

in the first variable A is set, since the blank key is not closed, command line will be interpreted as:
* A = B then hit <IFS>, then execute the command C
in the second A variable setting, since the soft quote is placed in the key blank, thus closed, is no longer as the IFS:
* A = B <Space> C
in fact, both in soft quote key blank or in the hard quote, will be shut down. Enter key versa:
        \ A $ = 'B
        > C
        >'
        \ $ echo "\ $ A"
        B
        C

In the above example, since the <enter> hard quote which is placed, and therefore will not be processed as the CR character.
Where <enter> symbol simply just a line break (new-line) only, since the command line did not get the character CR,
and therefore into the second shell prompt (PS2, to> notation), command line and does not end
until third row, we enter <enter> is not in the hard quote inside, so no is closed
at this time, command line CR character encountered, then the end shell to be processed.

Examples of the <enter>
        \ = $ A "B
        > C
        >"
        \ $ echo \ $ A
        the BC

However, since the echo \ $ A variable when placed in soft quote not, so after the replacement is completed and when the variable command line as recombinant, <enter> is interpreted as IFS, rather than interpreted as a New Line character.

Similarly, also closed with the escape character CR:
        \ $ A = B \
        > C \
        >
       \ $ echo \ $ A
        the BC

above example, the first <enter> with a second <enter> Close are the escape character , so as not to process the CR,
but the third <enter> Since not escape, so as CR end command line.
However, because of their special nature <enter> key shell meta is, the \ escape later, only CR cancel function, but does not maintain its function IFS.

You may find just one character <enter> key may be generated as these may have:
CR
the IFS
NL (New Line)
the FF (Form1 your feed)
NULL
...



        \ $ A = B \ C
        \ $ echo "\ $ A"
        the BC
        \ $ echo '\ A $'
        \ $ A

in the first echo command line, \ $ soft quote is placed in, will not be closed, Thus variable substitution process continues,
so the echo value of the variable a is output to the screen, will give the result "B C" of.
In the second echo command line, \ $ placed in hard quote, were closed, the \ $ just a \ $ symbol,
and will not be used for variable substitution process, so the result is \ $ a back SymbolPin A letter: \ $ A.

--------------------------------------
practice and thinking: The following findings are different?
        \ $ A = B \ C
        \ $ echo ' "\ $ A"' # outermost single quotes
        "\ $ A"
        \ $ echo " '\ $ A'" # outermost double quotes
        'B C'

Single and double quotes can close the shell handling of special characters. The difference is that there is no double quotes single quotes strict, single quote to close all of the characters have a special role, and double quotes only requires shell ignore most, specifically, is ① dollar sign ② ③ backslash backquote , these three special characters are not ignored. Do not ignore the dollar signs mean shell also replace the variable name inside double quotes.

Therefore, echo " '\ $ A'" # outermost double quotes

Inside the single quotes are closed, and the dollar sign is not closed, the variable body still cold, so the result is a single quotation marks variable values.

 

Guess you like

Origin www.cnblogs.com/aidata/p/11912647.html