Single and double quotes in shell script puzzle

Source: Internet
Author: User

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

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.