Shell Programming Bash quotes that thing _linux shell

Source: Internet
Author: User


First of all, in the bash script, there are three kinds of quotes



1. Single quotes '
2. Double quotes "
3. Inverted quotes '



Single quotation mark
The two string enclosed in single quotes is an ordinary string that retains its original literal meaning.
Double quotes
Two strings enclosed in double quotes, and some special characters will act on them.
These special characters are: Dollar sign $, backslash \, inverted quotation mark, exclamation point!.
Inverted quotes
A string surrounded by two inverted quotes that will run as a command,
The output is executed as the content of the inverted quotation mark, called the command substitution,
It has another better way of writing: $ (command)
Let's look at a few examples to see more directly the characteristics of these three quotes.



1. Dollar character $ in single or double quotes: in double quotes $, a variable reference will occur, whereas in single quotes the $ will retain its literal meaning





 code as follows:

Igi@gentoo ~ $ Echo ' $HOME '
$HOME
Igi@gentoo ~ $ echo "$HOME"
/home/igi





Note: Home is an internal variable



2. Backslash in single or double quotes: in double quotes, it will escape the character behind it, make it special meaning or lose its original special meaning, in single quotes, it will retain its literal meaning





 code as follows:

Igi@gentoo ~ $ Echo ' \ $HOME '
\ $HOME
Igi@gentoo ~ $ echo "\ $HOME"
$HOME





Note: in double quotes \, followed by $, there is an escape, which causes $ to lose its special meaning and become a normal character.



3. The inverted quotation marks differ from the other two quotes: the string enclosed by the inverted quotation mark will be run, and the result





 code as follows:

Igi@gentoo ~ $ Echo ' Date '
Date
Igi@gentoo ~ $ echo "Date"
Date
Igi@gentoo ~ $ Echo ' Date '
Fri Dec 3 18:34:09 CST 2010





Note: the date in the inverted quotation mark is executed as a command, containing exactly the output information of the command



To understand their differences, let's talk about common problems.



1. Put the inverted quotes ' into single quotes '



Have to say, they are really similar, individual book printing font difference is not high or printing quality is not clearance, resulting in a lot of novice admit, often put back quotes ' written in single quotes '. If you don't know where the inverted quotes are, look at the button below the ESC key, which is the inverted quote. Of course, also do not rule out that some people do not pay attention to reading, this is often the case. As long as we understand the difference between inverted and single quotes, when to use single quotes, when to use inverted quotes is clear. Use single quotes when you need a string, and use inverted quotes when you need to capture the output of the command.



2. Always forget to add double quotes
Double quotes are not always superfluous, and the data that is surrounded by it becomes safe enough to be cut off by bash.





 code as follows:

Igi@gentoo ~ $ seq 3
1
2
3
Igi@gentoo ~ $ Echo ' seq 3 '
1 2 3
Igi@gentoo ~ $ echo "' seq 3 '"
1
2
3





Note: SEQ outputs a message that contains a newline. but Echo ' seq 3 ' lost the line break because bash considered the output of ' seq 3 ' to be 3 separate characters when it was parsed, the same as Echo 1 2 3 (This process, bash does a lot of work, Interested in being able to understand the next Bash parsing order); While echo "' Seq 3 '", bash treats the output of ' seq 3 ' as a whole (because it is surrounded by double quotes), so the result of the output is not split by bash, and the line break is preserved. So, when you need to retain complete information about variables or command substitutions, especially line breaks, remember to put double quotes on them as insurance, and it's a good habit to add double quotes at all times (why not single quotes?). As explained earlier, there is no magic in single quotes, and variables are not changed, and command substitution is not replaced. Remind again: "$var", "command", are much safer than $var, ' command ', in many cases, the front is the result you want, unless you know what you are doing, do not easily omit double quotes.



3. Quote nesting is always confusing.



Quotation marks contain other quotes that are not difficult to grasp as long as you understand the characters in single and double quotes





 code as follows:

Igi@gentoo ~ $ echo "abc\" abc "
ABC "ABC"
Igi@gentoo ~ $ echo "abc\ ' abc"
ABC ' ABC
Igi@gentoo ~ $ echo "abc\ ' abc"
ABC ' ABC





As you can see, it's easy to add other quotes in double quotes, you just need to use a backslash to escape the quotes you want to add (double quotes with single quotes without escaping). Is it so simple in single quotes?




 code as follows:


Igi@gentoo ~ $ Echo ' abc ' ABC '
ABC "ABC"
Igi@gentoo ~ $ Echo ' abc ' ABC '
ABC ' ABC





Here, it is really very simple, single quotes are ordinary characters, so there is no need to escape, if added a backslash, then the backslash is still its own, directly printed out.





 code as follows:

Igi@gentoo ~ $ Echo ' abc\ ' \ ' abc '
Abc\ "\ ' ABC





Here's the question, how do you include single quotes in single quotes? At this point, the backslash is useless, and if you write single quotes directly, Bash thinks the quotes are not finished. Well, if you're unlucky enough to have a problem like this, there's a way out.





 code as follows:

Igi@gentoo ~ $ echo $ ' abc\ ' abc '
ABC ' ABC
Igi@gentoo ~ $ Echo-e ' ABC\X27ABC '
ABC ' ABC
Igi@gentoo ~ $ Echo ' abc ' \ ' abc '
ABC ' ABC





The first method is bash-specific, the $ ' string ' to the backslash will escape the character, the second method is to print single quotes through the ASCII code of single quotes, and the third method, by truncating the command, inserts a single quotation mark in the middle. In general, the first method is the most elegant.



Finally: Hopefully more people will like bash and use bash.


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.