In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the knowledge of "how to use Shell to program Bash quotation marks". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
First of all, there are three kinds of quotation marks in the bash script.
1. Single quotation mark'
two。 Double quotation marks "
3. Back quotation mark `
Single quotation mark
A string surrounded by two single quotes is an ordinary string that retains its original literal meaning.
Double quotation marks
A string surrounded by two double quotes, some special characters will play their role.
These special characters are: dollar sign $, backslash\, backquotation mark, exclamation point!.
Back quotation mark
A string surrounded by two backquotes that will be run as a command
The output of the execution is used as the content of the backquote, which is called command substitution
It has another better way to write it: $(command)
Let's look at a few examples to more directly understand the characteristics of these three quotation marks.
1. The expression of the dollar character $in single and double quotes: the $in double quotes will have a variable reference, while the $in single quotation marks will retain its literal meaning
The code is as follows:
Igi@gentoo ~ $echo'$HOME'
$HOME
Igi@gentoo ~ $echo "$HOME"
/ home/igi
Note: HOME is an internal variable
two。 The expression of backslash\ in single and double quotation marks:\ in double quotation marks will escape the character after it, make it have special meaning or lose its original special meaning, and\ in single quotation marks will retain its literal meaning.
The code is as follows:
Igi@gentoo ~ $echo'\ $HOME'
\ $HOME
Igi@gentoo ~ $echo "\ $HOME"
$HOME
Note:\ in double quotes, followed by $, is escaped so that $loses its special meaning and becomes an ordinary character.
3. The backquotation mark is different from the other two kinds of quotation marks: the string surrounded by the backquotation mark will be run and take its result.
The code is 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 backquotes is executed as a command and contains the output information of the command.
Now that we know their differences, let's talk about common questions.
1. Write back quotation marks as single quotation marks
It has to be said that they do look very much alike. The print differentiation of individual books is not high or the printing quality is not up to standard, resulting in many novices admitting their mistakes and often writing back quotation marks as single quotation marks. If you don't know where the backquotation mark is, please look at the button under the Esc key. That is the backquote. Of course, it does not rule out the possibility that some people do not pay attention to reading, which is also a common occurrence. As long as we understand the difference between backquotes and single quotes, it will be clear when to use single quotes and when to use backquotes. Use single quotes when you need a string, and backquotes when you need to capture the output of the command.
two。 Always forget to add double quotation marks
Double quotation marks are not always superfluous, and the data surrounded by it becomes secure from being cut open by bash.
The code is as follows:
Igi@gentoo ~ $seq 3
one
two
three
Igi@gentoo ~ $echo `seq 3`
1 2 3
Igi@gentoo ~ $echo "`seq 3`"
one
two
three
Note: seq outputs line breaks, but echo `seq 3` loses line breaks, because bash thinks that the output of `seq 3` is three independent characters, which is the same as echo 1 / 2 / 3. (bash has done a lot of work in this process, so if you are interested, you can know the parsing order of bash.) When echo "`seq 3`", bash treats the output of `seq 3` as a whole (because it is surrounded by double quotes), so the output will not be split by bash, and the newline character will be retained. So, when you need to keep complete information about variables or command substitutions, especially newline characters, remember to insure them with double quotation marks, and it's a good habit to put them in double quotes all the time (why not single quotes? As explained earlier, there is no magic in single quotes, variables cannot be changed, and command replacements cannot be replaced. Again, "$var" and "`command`" are much safer than $var, `command`. In many cases, the first is the result you want, and don't easily omit double quotes unless you know what you're doing.
3. Nesting of quotation marks is always confusing.
Quotation marks contain other quotation marks, as long as you can understand the performance of the characters in single and double quotation marks, it is not difficult to grasp
The code is 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 put other quotation marks in double quotation marks. You just need to use backslash to escape the quotation marks you want to add (single quotation marks in double quotation marks can not be escaped). So is it that simple in single quotation marks?
The code is as follows:
Igi@gentoo ~ $echo 'abc "abc'
Abc "abc
Igi@gentoo ~ $echo 'abc`abc'
Abc`abc
At this point, it is really simple, the single quotation marks are all ordinary characters, so there is no need to escape, if you add the backslash\, then the backslash is still its own, directly printed out.
The code is as follows:
Igi@gentoo ~ $echo 'abc\ "\ `abc'
Abc\ "\ `abc
The question is, how do you include single quotes in single quotation marks? At this point, the backslash\ is also useless. If you write single quotation marks directly, then bash will think that the quotation marks are not over. Well, if you have the misfortune to encounter such a problem, there is still a way to solve it
The code is as follows:
Igi@gentoo ~ $echo $'abc\' abc'
Abc'abc
Igi@gentoo ~ $echo-e'abc\ x27abc'
Abc'abc
Igi@gentoo ~ $echo 'abc'\' abc'
Abc'abc
This is the end of "how to program Bash quotes with Shell". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.