In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 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 spaces". Many people will encounter such a 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!
Find out when to use spaces and when not to use spaces in bash.
1. There can be no spaces on both sides of the equal sign assignment.
two。 A space is required between commands and options
3. The spaces on both sides of the pipeline are optional.
Let's take a look at common questions.
1. When assigning a value, there are spaces on both sides of the equal sign or only on the left.
Igi@gentoo ~ $var1 = testbash: var1: command not foundigi@gentoo ~ $echo ${var1:?error} bash: var1: errorigi@gentoo ~ $echo ${var1?error} bash: var1: errorigi@gentoo ~ $var2 = testbash: var2: command not foundigi@gentoo ~ $echo ${var2:?error} bash: var2: errorigi@gentoo ~ $echo ${var2?error} bash: var2: error
Here I use the variable extension of bash, ${var1:?error} report a specified error when var1 is unset or null (undefined or empty), and ${var1?error} report a specified error when var1 is unset. From the execution result, if there is a space to the left of the equal sign, the variable name is executed as a command, and the result is reported to command not found, and the variable is not assigned.
two。 When assigning an equal sign, there are no spaces on the left and spaces on the right (this case is a little special, you will find two cases)
Igi@gentoo ~ $var= testigi@gentoo ~ $var= nocmdbash: nocmd: command not found
There is also a space to the right of the equal sign, the first order is not wrong, and the second is wrong.
This is because there is a way to execute commands in shell: var=string command
The command command will get the value of the variable var (as for whether the value of the variable var is retained after the execution of the command, it is not retained in bash5, but different shell handles this differently when I find it in dash). Because test is a command, and nocmd is not, so reported to command not found.
Igi@gentoo ~ $var=newtest eval echo\ $varnewtestigi@gentoo ~ $echo $var
Note: I use eval here to avoid replacing $var with an empty string during the first parsing, otherwise the following situation will occur (the following is the wrong test method, $var has been replaced with an empty string before echo is executed)
The code is as follows:
Igi@gentoo ~ $var=newtest echo $var
Igi@gentoo ~ $echo $var
At this point, I believe we all understand, for the equal sign assignment, there can be no spaces on the left and right sides, although there is a space on the right does not necessarily report an error, but that is definitely not the result you want.
3. There must be a space between commands and options
Everyone seems to understand, why am I still so wordy? At this point, I have to mention a very special command: [command (you read it correctly, it's [), that is, the test command (of course, in bash, this is a built-in command, but it doesn't affect it here.
Our understanding). You may think [the command looks familiar, yes, I'm sure you've seen it. Let's take a look at the following example.
Igi@gentoo ~ $if ["abc" = "abc"]; then echo 'they are the same'; fithey are the sameigi@gentoo ~ $type-a [is a shell builtin [is / usr/bin/]
Do you remember? Commands are often used in if judgment, and of course some people like to write it.
Igi@gentoo ~ $["abc" = "cba"] | | echo 'they are not the same'they are not the sameigi@gentoo ~ $type-a [is a shell builtin [is / usr/bin/]
The real name of the command is test command. They are almost the same. Why are they not exactly the same? Take a look at this.
Igi@gentoo ~ $["abc" = "cba" bash: [: missing `] 'igi@gentoo ~ $["abc" = "cba"] igi@gentoo ~ $test "abc" = "cba"] bash: test: too many argumentsigi@gentoo ~ $test "abc" = "cba"
To be clear, you must give it a tail when you use the command, and you can't add a tail when you use the test command. Tail] is the last parameter, an indispensable parameter, which represents the end of the command.
With all this talk, what does this have to do with spaces? To say this is to make everyone understand: "it is a command in shell, and there must be spaces around it!" Is the last indispensable parameter of [, and it also requires spaces on both sides (although the arguments of some commands can be concatenated, such as ps, the command cannot, there must be spaces between its arguments). Let's take a look at common mistakes.
A. lack of space between if and [
Igi@gentoo ~ $if ["$HOME" = "/ home/igi"]; then echo 'equal'; fibash: syntax error near unexpected token `then'igi@gentoo ~ $if ["$HOME" = "/ home/igi"]; then echo' equal'; fibash: syntax error near unexpected token `then'igi@gentoo ~ $if ["$HOME" = "/ home/igi"]; then echo 'equal'; fibash: syntax error near unexpected token `then'igi@gentoo ~ $if ["$HOME" = "/ home/igi"]; then echo' equal' Fibash: syntax error near unexpected token `then'
Parsing error, obviously, if [for bash, I don't know what the hell it is.
b. [missing space between and the following parameters
Igi@gentoo ~ $if ["$HOME" = "/ home/igi"]; then echo 'equal'; fibash: [/ home/igi: No such file or directoryigi@gentoo ~ $if ["$HOME" = "/ home/igi"]; then echo' equal'; fibash: [/ home/igi: No such file or directory
["$HOME" to bash, I don't know what the heck it is
C. Missing spaces between parameters between []
Igi@gentoo ~ $if ["abc" = "abc"]; then echo 'equal'; fiequaligi@gentoo ~ $if ["abc" = "cba"]; then echo' equal'; fiequal
The first command seems to be correct (it's actually just a coincidence). Look at the second command, "abc" and "cba" are obviously different, but judged to be the same. This is because there is a lack of space between the parameters, which is considered by the command to be an internal value. Take a look at the following orders and you will be relieved
Igi@gentoo ~ $if [0]; then echo 'equal'; fiequaligi@gentoo ~ $if ["1"]; then echo' equal'; fiequaligi@gentoo ~ $if ["]; then echo 'equal'; fiigi@gentoo ~ $if []; then echo' equal'; fi
Inside [], if there is only one value (those that are joined together because of the lack of spaces), it is true if it is not an empty string. So the parameters between [] should also have spaces on both sides, not be stacked together.
d. Missing space between parameter and tail]
This is not wordy, tail] is also a parameter of the command, as mentioned above, there must be spaces between the parameters
There are so many things about commands and spaces, but sometimes they work correctly without spaces. Of course, it's just your luck. Let's take a look.
Igi@gentoo ~ $var=' abc'igi@gentoo ~ $if [$var= "abc"]; then echo 'equal'; fiequaligi@gentoo ~ $if ["$var" = "abc"]; then echo' equal'; fibash: [abc: command not found
As mentioned earlier in the case of Bash quotes, double quotes are surrounded by a whole, and when there are no double quotes, the spaces or tabs before and after the string are cut. If you happen to encounter or deliberately discard spaces or tabs before and after a string, it's not impossible, but it's highly discouraged and your code will be very fragile.
Or you may add all the spaces you should add, but still report an error, which may also have something to do with the lack of double quotation marks. This kind of situation is very common. Let's take a look at it at last.
Igi@gentoo ~ $var=''igi@gentoo ~ $if ["$var" = "abc"]; then echo 'equal'; fiigi@gentoo ~ $if [$var= "abc"]; then echo' equal'; fibash: [: unary operator expectedigi@gentoo ~ $dvar='a bc 'igi@gentoo ~ $if [$dvar= "abc"]; then echo' equal'; fibash: [: too many argumentsigi@gentoo ~ $if ["$dvar" = "abc"]; then echo 'equal' This is the end of fiequal's "how to program Bash spaces 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.