Cu Play Shell 13 question: "" (Double Quotes) and '' (single number)?

xiaoxiao2021-03-06  68

Or return to our Command Line ... After the two chapters, it should be very clear when you hit the keyboard behind the shell prompt until the Enter is pressed, the text you entered is Command Line, and then shell The command you handed over with the itinerary. However, you can know: Every text you entered in Command Line, is there a category of the shell? Simple (I don't dare to say this is accurate to expose, note one), each Charactor of Command Line is divided into the following two: * literal: It is ordinary pure text, there is no special function for the shell. * Meta: Special reserves with specific functions for Shell. (Note: About Bash Shell's order description when processing Command Line, please refer to O'Reilly Press Learning The Bash Shell, 2nd Edition, No. 177 - 180 Page, especially on page 17 Foundation Figure 1 ...) Litral has no tang, where ABCD, 123456 these "text" are literal ... (easy?) But Meta often makes us confused ..... (confused?) In fact, before Two chapters we have encountered two machines that have encountered two machines in Command Line, which is composed of or or three of the three (we often use Space). * CR: Produced by . IFS is used to disassemble each word (Word) of Command Line, because shell Command Line is processed by word. The CR is used to end Command Line, which is why we knock command will run. In addition to IFS and Cr, common METAs are also: =: Set variables. $: Make a variable or an alternative (please don't confuse the shell prompt). >: Heavy directive stdout. <: Heavy guidance stdin. |: Command line. &: Keep File Descriptor, or place the command to the back. (): Place the inside the command to the NESTED SUBSHELL, or for the operation or command replacement. {}: Place it in the Non-Named function, or uses the defined range of variables. : At the end of the previous command, ignore its return value and continue the next command. &&: If the previous command ends, if the value is true, continue to perform the next command. ||: When the previous command ends, if the return value is false, continue to execute the next command. !: Execute the command in the History list .... If we need to close these reserved characters in Command Line, you need quoting to handle it.

In Bash, common quoting has three methods: * Hard quote: '' (single number), all METAs in Hard Quote are closed. * Soft Quote: "" (Double Quotes), large part of Meta in Soft QuoE will be turned off, but some are reserved (such as $). (Note) * escape: / (reverse slope), only close Meta immediately after Escape (Jumk) is closed. (Note: In the case of the specific meta list of Soft Quote, I don't fully know that if you need to make up, or through the practice to find and understand.) The following example will help our understanding: Code: $ A = BC # blank key is not turned off, as IFS processing. $ C: Command Not Found. $ ECHO $ ​​A $ A = "B C" # blank key has been turned off, only a blank key process. $ Echo $ A B C

When setting the A variable for the first time, since the blank key is not closed, Command Line will be interpreted as: * a = b and then touch , and then execute the c command When set a variable for the second time, due to blank key Placed in Soft Quote, is therefore closed, no longer as IFS: * a = b C In fact, blank keys are closed in Soft Quote or in Hard Quote, it will be turned off. ENTER key is also:

Code: $ a = 'b> c>' $ echo "$ a" b c

In the above example, since is placed in the Hard Quote, it is no longer processed as a Cr character. Here's is just a broken line symbol (new-line), because Command Line does not get a CR character, so enter the second shell prompt (PS2, in> symbolic representation), Command Line will not end, Until the third line, we entered is not in the hard quote, there is no shutdown, at this time, Command Line hits the CR character, which is ended, handed over to the shell. If the of the above example is placed in Soft Quote, the Cr is also closed:

Code: $ a = "b> c>" $ ECHO $ ​​A B C

However, since the variables at ECHO $ ​​A are not in Soft Quote, when the variable is completed, will be interpreted as IFS, not an explanation as a New Line character. Similarly, use Escape can also close the Cr character:

Code: $ a = b /> c /> $ ECHO $ ​​A BC

In the above example, the first is closed by the escape character with the second , so it is not processed as a CR, but the third is not jumped, so as the CR end Command line. . However, because the key itself is specialty in Shell Meta, after / jumping, only its CR function is canceled without retaining its IFS function. You may find that the characters generated by the light is a key may be these possible: Cr IFS NL (new line) ff (form feed) NULL ... as for when to explain why characters, this is not I have dug deeply, or leave it to the reader, I will slowly explore ... ^ _ ^ As for Soft Quote, the hard quote is different, mainly for some Meta closes, with a description: Code: $ A = b / c $ echo "$ a" BC $ Echo '$ A' $ A

In the first echo command line, the $ Soft Quote will not be closed, thus continuous processing variable replacement, so the ECHO outputs A to the screen, which also obtains the result of "B C". In the second echo command line, the $ is placed in the hard quote, so $ is just a $ symbol, and it is not used to make a variable replacement process, so the result is the next A letter behind: $ A. -------------------------------------- Practice and Thinking: Why is the following results?

Code: $ a = b / c $ echo '"$ a"' # The outermost is single quotes "$ a" $ echo "'$ a'" # outer is double quotes 'bc' (Tip: single quotes And double quotes, are all closed in quoting? # 93 ;.)

-------------------------------------- In the shell version of CU, I found a lot of beginners. The problem is related to the QUOTING. For example, if we call some variables before the parameters of the AWK or SED, will often ask why it is impossible. To resolve these issues, the key points are: * Distinguish the Meta we mentioned in front of Shell Meta and Command Meta, all of which have special purposes in Command Line, ratio {} is placed in one series of Command Line The unnamed functions (can be simply treated as Command Block), but AWK needs to distinguish the AWK command segment (Begin, Main, End) with {}. If you enter this in Command Line:

Code: $ awk {print $ 0} 1.txt

Since {} is not closed in the shell, the shell will treat {Print $ 0} as a Command Block, but at the same time, there is no ";" symbol as a command area, therefore, the AWK's syntax error result. To solve it, use hard quote:

Code: $ awk '{print $ 0}' 1. The hard quote above the TXT should be understood, which is to close the original {, , $ (note three),}, these shell meta, avoid falling in the shell It was processed, and completely became a Command Meta in the AWK parameter. (Note Three: The $ 0 is the Field Number built by AWK, not the AWK variable, the AWK own variable does not need to use $.) If you understand the Hard Quote function, then understand the Soft Quote and Escape is not difficult:

Code: awk "{print / $ 0}" 1.txt awk / {print / / / $ 0 /} 1.txt

However, if you want to change the 0 value of the $ 0 in AWK is read from another shell variable? For example: The value of the variable $ A is 0, how do you solve the AWK $$ in Command Line? You can directly deny the Solution of Hard QuoE:

Code: $ awk '{print $$ a}' 1.txt

That is because $ A of $ A is not replaced in Hard Quote. Smart readers (such as you!), After learning this chapter, I think, you should explain why we can use the following:

Code: A = 0 awk "{print / $$ A}" 1.txt awk / {print / / $$ A /} 1.txt awk '{print $' $ A '}' 1.txt awk '{print $ '"$ A"'} '1.txt # Note: "$ a" package in Soft Quote

Perhaps, you can mention more solutions .... ^ _ ^

转载请注明原文地址:https://www.9cbs.com/read-90321.html

New Post(0)