Spaces in Bash

Source: Internet
Author: User

Let's take a look at the rules for using spaces in bash:

1. No spaces are allowed on both sides of the = sign for assignment operations.

2. A space is required between the command and the option.

3. The space on both sides of the pipe is optional.


Take a look at the various causes of errors due to whitespace:

1) The assignment operation has a space at the left of the = sign

var = "abc"
The resulting error message is:

Var:command not found

This is because when bash interprets the command, VAR is used as a command to find it in the system's path. Once you cannot find the var command, you are prompted to "do not find the var command", then the subsequent statements will not be executed.

2) The assignment operation has a space on the right of the = sign

Var= "ABC"

The resulting error message is:

Abc:command not found

This is because there is a way to execute the command in bash: var=string command

Command commands will get the value of variable var, then "ABC" will be interpreted as a command by bash, then the returned information is of course "cannot find ABC this command".

3) The assignment operation has a space around the = sign.

This is the case in the first case.


4) No spaces are used between commands.

There are several common scenarios:

"If and [no spaces between]"

if[-N "abc"];then Echo Ok;fi

Cause of error: Many people think [] in bash is just a symbol that judges Boolean values. In fact [] the role in bash is equivalent to test, which is of course a command.

Then if is a stand-alone command, [] is also a separate command. Two commands of course there should be a space, otherwise, like if[, bash can only be understood as a whole, and thus error.


"[XXX] where the arguments have no spaces at both ends"

if ["abc" = "abc"];then Echo Ok;fi

Cause of the error: the same bash simply doesn't understand what ["abc" is. Remember [] not just a symbol, but a command.


"[" "abc" = "CBA"] there are no spaces between the parameters "

if ["abc" = "abc"];then Echo Ok;fi

Cause of Error:

= There are spaces on both sides, which is an expression that determines whether the values on both sides are equal and returns a bool value.

= Missing spaces on both sides, bash will "abc" = "CBA" as a whole, it always returns to true.


As for the pipeline, although the space on both sides is optional, but in order to beautiful, generally still keep both sides of the space.



Spaces in Bash

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.