Work in common to shell scripts, encountered a doubt, solve.
A common explanation:
Single-Quote String restrictions:
- Any character in a single quotation mark is output as is, and the variable in the single-quote string is not valid;
- A single quotation mark cannot appear in an apostrophe string (not after using the escape character for single quotes), but it can be paired and used as a string concatenation.
Advantages of double quotes:
- You can have variables in double quotes.
- Escape characters can appear in double quotes
My question one: In the Vim editor, Line 21st, in the case of single quotation marks, the ' $i ' is printed out of the value, not the single quotation mark in the above explanation is invalid, print out $i.
Figure A
A similar script, denoted by double quotation marks: line 18th
6~9 Four line is str.txt content (copy to show together for easy Viewing)
Figure II, Output script
Double quotes, single quotes, no quotes results such as: red circle up the part. The result is:
A variable enclosed in double quotation marks, which outputs the nineth line of Figure two as-is, * does not work in double quotes
A single quote is indeed any character that is output as it is, outputting the $word
Without the quotation marks, the first letter in the Nineth line of Figure II * Works, outputting the contents of this directory.
Doubt 1: In Figure one, the variable in the single quotation mark should be invalid but the result prints the value, which is invalid in Figure two, because the single quotation mark in Figure one is used in the awk command line.
Doubt 2: In Figure two, double quotes output and no quotation marks output, double quotation marks after the execution of "$word", print out * Procedure Status *, echo execution, not first hit the * symbol, first execute it? Why is it not implemented here? Doesn't it mean that you can have variables in double quotes? One problem here is the * symbol, which is explained here as whether it belongs to a variable.
The task was done, but the doubt, like a thorn stuck in the heart uncomfortable, solved.
Single and double quotes in shell script puzzle