Home > php教程 > PHP开发 > Shell Programming: Bash Space Things

Shell Programming: Bash Space Things

高洛峰
Release: 2016-11-23 12:57:28
Original
1260 people have browsed it

First understand when to use spaces in bash and when not to use them.

1. There cannot be spaces on both sides of the equal sign assignment

2. Spaces are required between commands and options

3. Spaces on both sides of the pipe are optional

Let’s take a look at common problems

1. Equal signs when assigning values There are extra spaces on both sides or only on the left
test@pythontab.com ~ $ var1 = test
bash: var1: command not found
test@pythontab.com ~ $ echo ${var1:?error}
bash: var1: error
test @pythontab.com ~ $ echo ${var1?error}
bash: var1: error
test@pythontab.com ~ $ var2 =test
bash: var2: command not found
test@pythontab.com ~ $ echo ${var2 :?error}
bash: var2: error
test@pythontab.com ~ $ echo ${var2?error}
bash: var2: error

Here I used bash variable expansion, ${var1:?error} when When var1 is unset or null (undefined or empty), the specified error is reported; ${var1?error}When var1 is unset, the specified error is reported. Judging from the execution results, if there is a space on the left side of the equal sign, the variable name is executed as a command, and the result is command not found, and the variable is not assigned. 2. When assigning a value, there is no space on the left side of the equal sign, but there is a space on the right side (this situation is a bit In particular, you will find two situations)
test@pythontab.com ~ $ var= test
test@pythontab.com ~ $ var= nocmd
bash: nocmd: command not found

There is also a space on the right side of the equal sign. One command did not report an error, but the second one did.

This is because there is such a way to execute commands in the 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 command is executed, it is not retained in bash4, But I kept it when I found it in dash. Different shells handle this differently). Since test is a command and nocmd is not, I reported command not found.
test@pythontab.com ~ $ var=newtest eval echo $var
newtest
test@pythontab.com ~ $ echo $var

Note: I used eval here to avoid $var being replaced with an empty string during the first parsing, otherwise the following situation will occur (The following is the wrong test method. Before echo is executed, $var has been replaced with an empty string)

The code is as follows:
test@pythontab.com ~ $ var=newtest echo $var
test@pythontab.com ~ $ echo $var

At this point, I believe everyone understands that for equal sign assignment, there cannot be spaces on the left and right sides. Although spaces on the right may not necessarily cause an error, that is definitely not the result you want.

3. There must be a space between the command and the option

Everyone seems to understand this, why am I still so wordy? Speaking of which, I have to mention a very special command: [ command (you read that right, it is [ ), which is the test command (of course in bash, this is a built-in command, but it does not affect our understanding here) ). Maybe you will think that the [command looks familiar, yes, I guarantee you have seen it, take a look at the following example
test@pythontab.com ~ $ if [ "abc" = "abc" ]; then echo 'they are the same '; fi
they are the same
test@pythontab.com ~ $ type -a [
[ is a shell builtin
[ is /usr/bin/[

Remember? [Commands are often used in if judgments. Of course, some people like to write them like this
test@pythontab.com ~ $ [ "abc" = "cba" ] || echo 'they are not the same'
they are not the same
test @pythontab.com ~ $ type -a [
[ is a shell builtin
[ is /usr/bin/[

[ The command is called test command. They are almost the same. Why are they not exactly the same? Let’s take a look at this
test@pythontab.com ~ $ [ "abc" = "cba"
bash: [: missing `]'
test@pythontab.com ~ $ [ "abc" = "cba" ]
test@pythontab .com ~ $ test "abc" = "cba" ]
bash: test: too many arguments
test@pythontab.com ~ $ test "abc" = "cba"

It’s clear, when using the [command, you must Give it a tail], when using the test command, you cannot add a tail. Tail] is [the last parameter, an indispensable parameter, which represents the end of the command

I have said so much, but what does this have to do with spaces? Let me say this first to let everyone understand: [It is a command in the shell, and there must be spaces around it! ] is the last indispensable parameter of [, which also requires spaces on both sides (although the parameters of some commands can be connected together, such as ps, the [ command cannot, there must be spaces between its parameters). Let's look at [common mistakes

a. Missing space between if and [
test@pythontab.com ~ $ if[ "$HOME" = "/home/igi"]; then echo 'equal'; fi
bash: syntax error near unexpected token `then'
test@pythontab.com ~ $ if[ "$HOME" = "/home/igi" ];then echo 'equal'; fi
bash: syntax error near unexpected token ` then'
test@pythontab.com ~ $ if["$HOME" = "/home/igi"];then echo 'equal'; fi
bash: syntax error near unexpected token `then'
test@pythontab.com ~ $ if["$HOME" = "/home/igi" ];then echo 'equal'; fi
bash: syntax error near unexpected token `then'

Syntax analysis error, obviously, if[ For bash, I don’t know what the hell it is

b. There is a lack of space between [ and the following parameters
test@pythontab.com ~ $ if ["$HOME" = "/ home/igi" ];then echo 'equal'; fi
bash: [/home/igi: No such file or directory
test@pythontab.com ~ $ if ["$HOME" = "/home/igi"]; then echo 'equal'; fi
bash: [/home/igi: No such file or directory

["$HOME" For bash, I don’t know what the hell it is

c. [ ] The parameters between Missing space between
test@pythontab.com ~ $ if [ "abc"="abc" ]; then echo 'equal'; fi
equal
test@pythontab.com ~ $ if [ "abc"="cba" ] ; then echo 'equal'; fi
equal

The first command seems to be correct (actually it is just a coincidence), look at the second command "abc" and "cba" are obviously different, but they are judged to be the same. This is because there is a lack of space between the parameters, and the [command considers the internal value to be just a value. Take a look at the following command and you will be relieved
test@pythontab.com ~ $ if [ 0 ]; then echo 'equal'; fi
equal
test@pythontab.com ~ $ if [ "1" ]; then echo ' equal'; fi
equal
test@pythontab.com ~ $ if [ "" ]; then echo 'equal'; fi
test@pythontab.com ~ $ if [ ]; then echo 'equal'; fi

in [ ] Internally, true if there is only one value (those concatenated by missing spaces are also counted) that is not the empty string. Therefore, the parameters between [ ] should also have spaces on both sides, and cannot be stacked together

d. There is a lack of space between parameters and tail]

This is not verbose, tail] is also a parameter of [command, as above As mentioned, there must be spaces between parameters

So much about [commands and spaces], but sometimes, it can run correctly without spaces. Of course, this is just your good luck. Let’s take a look
test@pythontab. com ~ $ var=' abc'
test@pythontab.com ~ $ if [$var = "abc" ]; then echo 'equal'; fi
equal
test@pythontab.com ~ $ if ["$var" = "abc" ];then echo 'equal'; fi
bash: [ abc: command not found

Double quotation marks surround a whole, and when there are no double quotation marks, the spaces or tabs before and after the string are Cut. If you happen to encounter it or you deliberately want to discard spaces or tabs before and after the string, it is not impossible, but it is highly not recommended that you write this, your code will be very fragile.

Or you have added all the spaces you should add, but still get an error. This may also be related to the lack of double quotes. This situation is very common, take a look at it finally
test@pythontab.com ~ $ var=''
test@pythontab.com ~ $ if [ "$var" = "abc" ];then echo 'equal'; fi
test@pythontab.com ~ $ if [ $var = "abc" ];then echo 'equal'; fi
bash: [: =: unary operator expected
test@pythontab.com ~ $ dvar='a b c'
test @pythontab.com ~ $ if [ $dvar = "a b c" ];then echo 'equal'; fi
bash: [: too many arguments
test@pythontab.com ~ $ if [ "$dvar" = "a b c" ] ;then echo 'equal'; fi
equal

I'll be verbose again, don't omit the double quotes easily. Is it clear? If you still don’t understand,

Finally, I won’t talk nonsense about the optional spaces on both sides of the pipe, because I haven’t met anyone who has any doubts about this.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template