Shell programming: The point of the bash space _linux Shell

Source: Internet
Author: User
Tags builtin eval

Know when to use a space in bash and when not to use it.

1. Equal value assignment cannot have spaces on either side
2. Spaces are required between commands and options
3. Pipe on both sides of the space is optional

Let's take a look at the common problems.

1. The equal sign on either side or only the left is more space

Igi@gentoo ~ $ var1 = Test
bash:var1:command not found
Igi@gentoo ~ $ echo ${var1:?error}
bash:var1:error< C5/>igi@gentoo ~ $ echo ${var1?error}
bash:var1:error
igi@gentoo ~ $ var2 =test Bash:var2:command not
Foun D
Igi@gentoo ~ $ echo ${var2:?error}
bash:var2:error
igi@gentoo ~ $ echo ${var2?error}
Bash:var2:er Ror

Here I'm using Bash's variable extensions, ${var1:?error} when var1 is unset or null (undefined or empty), the specified error is reported; ${var1?error} When Var1 is unset, the report specifies an error. From the execution result, if there is a space on the left of the equal sign, the variable name executes as a command, and the result is not found, and the variable is not assigned.

2. When assigned, there is no space on the left side of the equal sign, there are spaces on the right side (this is a bit special, you will find two situations)

Igi@gentoo ~ $ var= Test
Igi@gentoo ~ $ var= nocmd
Bash:nocmd:command not found

The same sign has a space on the right, the first command does not complain, and the second error.
This is because there is a way to execute commands in the shell: var=string command
Command commands will get the value of the variable Var (as to whether the value of Var is preserved after the command has been executed, BASH4 is not retained in the dash, but I have preserved it when I found it in the dash), because test is a command and nocmd is not, So I called command not found.

Igi@gentoo ~ $ var=newtest eval echo \ $var
newtest
igi@gentoo ~ $ echo $var

Note: Here I use eval to avoid $var being replaced with an empty string at the first parse, or else the following situation occurs (the following is the wrong test method, $var has been replaced with an empty string when echo has not been executed)

Copy Code code as follows:

Igi@gentoo ~ $ var=newtest echo $var
Igi@gentoo ~ $ echo $var

Here, I believe we all understand it, for equal value assignment, the left and right sides can not have spaces, although there is no space on the right-hand side does not necessarily error, but that is definitely not the result you want.

3. There must be a space between the command and the option
This seems to everyone understand why I still so wordy? In this case, we have to mention a very special command: [the command (you did not read the wrong, is [), the Test command (of course bash, this is a built-in command, but here does not affect
Our understanding). You might think [commands look familiar, yes, I guarantee you've seen it, see the example below

Igi@gentoo ~ $ IF ["abc" = "abc"]; Then echo ' They are the same '; Fi
They are the same
Igi@gentoo ~ $ type-a [
[is a shell builtin [
is/usr/bin/[

Come to think of it? [commands are often used in if judgments, and of course some people like to write them.

Igi@gentoo ~ $ ["abc" = "CBA"] | | Echo ' They are not the same '
they are not the same
Igi@gentoo-$ type-a [
[is] a shell builtin
[is/us r/bin/[

[command is named Test command, they are almost the same, why not exactly the same? Let's see this.

Igi@gentoo ~ $ ["abc" = "CBA"
bash: [: Missing '] '
igi@gentoo ~ $ ["abc" = "CBA"]
Igi@gentoo ~ $ test "a BC "=" CBA "]
bash:test:too many arguments
Igi@gentoo ~ $ TEST" abc "=" CBA "

It is clear that, with [the command, you must give it a tail], the test command, you can not add a tail. Tail] is [last parameter, an indispensable parameter, representing the end of the command

After all this, what does this have to do with the blanks? To say this, is to let everyone understand: [in the shell is a command, it must have a space around! Is the last indispensable parameter, which requires a space on both sides (although some command parameters can be joined together, such as PS, but the command is not available, and there must be spaces between its arguments). Let's take a look at [common bugs]

A. If and [missing space between

Igi@gentoo ~ $ if["$HOME" = "/home/igi"];then echo ' equal '; Fi
bash:syntax error near unexpected token ' then ' Igi@gentoo
~ $ if["$HOME" = "/home/igi"];then echo ' equal '; f I
bash:syntax error near unexpected token ' then ' Igi@gentoo
~ $ if["$HOME" = "/home/igi"];then echo ' equal '; fi
   bash:syntax error near unexpected token ' then ' Igi@gentoo
~ $ if["$HOME" = "/home/igi"];then echo ' equal '; fi
   
    bash:syntax error near unexpected token ' then '
   

Parsing error, obviously, if[for bash, I don't know what the hell it is.

b. [missing spaces between the parameters that follow]

Igi@gentoo ~ $ IF ["$HOME" = "/home/igi"];then echo ' equal '; Fi
bash: [/home/igi:no such file or directory
Igi@gentoo ~ $ 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. [] missing space between parameters

Igi@gentoo ~ $ IF ["abc" = "abc"]; Then echo ' equal '; Fi
equal
igi@gentoo ~ $ IF ["abc" = "CBA"]; then echo ' equal '; fi
Equal

The first command seemed to be right (in fact it was just a coincidence) to see that the second command, "ABC" and "CBA", were distinctly different, but judged the same way. This is because there is a lack of space between the arguments, and the command considers the interior to be a value. Look at the commands below and you'll be relieved.

Igi@gentoo ~ $ if [0]; Then echo ' equal '; Fi
equal
igi@gentoo ~ $ IF ["1"]; then echo ' equal '; fi
equal
igi@gentoo ~ $ if ["]; Then echo ' equal '; Fi
Igi@gentoo ~ $ if []; then echo ' equal '; fi

Within [], if there is only one value (those that are joined together because of the absence of a space), it is not the empty string that is true. So in [] between the parameters, there are also spaces on both sides, but not the heap together

D. Missing space between parameter and tail]
This is not wordy, the tail is also a parameter of the command, as mentioned above, there must be a space between the parameters

Pull up so much [command with the space thing, but sometimes, lack of space but can run correctly, of course it's just your good luck, together to see

Igi@gentoo ~ $ var= ' abc '
igi@gentoo ~ $ if [$var = "abc"];then Echo ' equal '; fi
equal
igi@gentoo ~ $ if ["$v Ar "=" abc "];then Echo ' equal '; Fi
bash: [Abc:command not found

As mentioned in the previous bash quotes, double quotes surround a whole, and when there are no double quotes, the spaces or tabs before and after the string are cut open. If you happen to encounter or you deliberately discard the string before and after the space or tab, it is not impossible, but very not recommended that you write, your code will be very vulnerable.

Or you should add the space is added, but still error, which may be missing double quotes. This is a very common situation, and finally look at

Igi@gentoo ~ $ Var= '
Igi@gentoo ~ $ if ["$var" = "abc"];then Echo ' equal '; Fi
igi@gentoo ~ $ if [$var = "abc" ];then echo ' equal '; Fi
bash: [: =: unary operator expected
Igi@gentoo ~ $ Dvar= ' a b C '
Igi@gentoo ~ $ if [$dvar = "a b C"];then echo ' equal '; Fi
bash: [: Too many arguments
Igi@gentoo ~ $ if ["$dvar" = "a b C"];then echo ' equal '; fi
Equal

I repeat, do not easily omit the double quotes. It's clear, isn't it? If you don't get it,
Read the bash quotes, please.

Finally, for the pipeline on both sides of the optional space, it is not nonsense, because there are no doubts about it.

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.