The difference between grep plus single, double, and no quotes

Source: Internet
Author: User
Tags constant final

Environment: VirtualBox4.1 + RedHat Linux 6 + Putty0.62

Single quotation mark:

What you can say is what you see: The output of the contents of the single quotation mark, or the output of what is seen in single quotes. Single quotes ' are full references, and the contents of the enclosed quotes are not replaced by constants or variables.

Double quotes:

Output the contents of the double quotes; if there are commands, variables, etc., the variables and commands are parsed and then the final content is output. Double quotes "" is a partial reference, a content constant that is enclosed in double quotes, or a constant, and a variable that replaces the variable's contents.

No quotes:

Strings that contain spaces are not treated as one overall output, if the content has commands, variables, and so on, will first the variables, the command to resolve the results, and then in the output of the final content, if the string with space and other special characters, you can not complete the output, need to change the double quotes, general continuous strings, numbers, paths, etc.

Use rules:

The general constants are enclosed in single quotes ' and enclose them in double quotes if they contain variables.

Max difference:

The biggest difference between single and double quotes is that double quotes can still hold variable content, but single quotes can only be generic characters without special symbols

Use examples:

"" "There are $,\ and other special characters encountered specifier to replace the corresponding variable

All the characters in the ' number are intact.

For strings, the two are the same

Matching patterns are roughly the same

But there are some differences that are easily confusing

grep "$a" file #引用变量a to find the value of variable a

grep ' $a ' file #查找 ' $a ' string---www.bianceng.cn

grep "\" file #grep: Trailing backslash (unknown reason)

grep ' \ ' file #查找 ' \ ' character

1, $ dollar character

2, reverse Slash

3, ' inverted quotation mark

4, "double quotes

These four characters have special meanings in double quotes, others are not, and single quotes make all characters lose their special meaning

If you use double quotes to find a \, you should use four \:

grep "\\\\" file is the right thing to do, which is equivalent to:

grep ' \ ' file

The first command shell four \, escaped into 2 \ passed to Grep,grep and then 2 \ escaped into a \ Lookup

The second command shell is not escaped, the 2 pass directly to the Grep,grep and then 2 \ escaped into a \ Lookup

In fact, grep executes the same command.

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.