I encountered a problem today.
How does awk print single quotes and double quotes?
[[email protected] ~]# echo | awk ‘{print "\"" }‘ "
Or
[[email protected] ~]# echo |awk ‘{print "\x22"}‘"
[[Email protected] ~] # Echo | awk "{print \" '\ "}" # prefer this method, which is simple and easy to understand'
Or
[[email protected] ~]# echo |awk ‘{print "‘\‘‘"}‘ ‘
Or
[[email protected] ~]# echo |awk ‘{print "\x27"}‘‘
Change to awk '{print "'\''"}'
Switch
Awk '{print "'
\'
'"}'
Awk "{print \"'\"}"
Awk "{print \"
'
\"}"
The three special characters enclosed in double quotation marks are not ignored: $, \, ', that is, double quotation marks explain the special meaning of strings, while single quotation marks directly use string output.
Be familiar with the differences between single quotation marks and double quotation marks when handling special characters
So how does shell handle special characters?
1. Single quotes ('')
If we want to search for Susan Goldberg, we cannot directly use the grep Susan Goldberg phonebook command. grep regards Goldberg and phonebook as files to be searched.
[Email protected] [script] $ grep 'Susan gold' phonebook
Susan Goldberg 403-212-4921
When the shell encounters the first single quotation mark, it ignores all the special characters that follow it until the right quotation mark.
2. Double quotation marks ("")
Double quotation marks are similar to single quotes. The difference is that they are not so strict. Single quotes tell shell to ignore all special characters, while double quotes only need to ignore the majority. Specifically, three special characters enclosed in double quotes are not ignored: $ ,\,', double quotation marks explain the special meaning of strings, while single quotation marks directly use strings. if you use double quotation marks to assign a string to a variable and feed it back, it is actually no different from the direct feedback variable. Double quotation marks are often used to query strings containing spaces.
[Email protected] [script] $ x = *
[Email protected] [script] $ echo $ x
Hello. Sh menus. Sh Misc. Sh phonebook tshift. Sh
[Email protected] [script] $ echo '$ x'
$ X
[Email protected] [script] $ echo "$ X"
*
In this example, we can see the difference between no quotation marks, single quotation marks, and double quotation marks. In the last case, double quotation marks Tell Shell to replace the variable name in the quotation marks, so shell replaces $ X with * because the double quotation marks do not replace the file name, therefore, * is passed to echo as the value to be displayed.
For the first case, it should be further explained that shell does not replace the file name when assigning values to variables (this can also be seen in the third case). The exact sequence of each step is as follows:
Shell scans the command line and sets the value of X to asterisk *;
Shell scans the command line again, encounters the asterisk *, replaces it with the file list in the current directory;
Shell starts to execute the echo command and passes the file list as a parameter to Echo.
The order of value assignment is very important: Shell replaces the variable first, then the file name, and finally processes this line as a parameter.
3. Reverse quotation marks ('')
Command replacement means that shell can insert the standard output of a command to any location in a command line. There are two methods in shell to replace the command: Use reverse quotation marks or $ (...) for shell commands (...) structure, where $ (...) the format is supported by the POSIX standard and also facilitates nesting.
[Email protected] [script] $ echo the date and time is 'date'
The date and time is three June 15 06:10:35 CST 2005
[Email protected] [script] $ echo your current working directory is $ (PWD)
Your current working directory is/home/Howard/script
4. backslash-escaped (\)
The backslash is generally used as an escape character or escape character. In Linux, if ECHO is used to enable the escape character, the-e option must be used and double quotation marks must be used for escape characters.
Echo-e "\ n"
Another function of the backslash is that when the backslash is used for the last character of a line, shell uses the backslash at the end of the line as the continuation line, this structure is often used when several lines of long commands are input.
Awk print single quotes and double quotes