Spaces in bash

Source: Internet
Author: User

Spaces in bash

Let's take a look at the space usage rules in bash:

1. During the value assignment operation, spaces are not allowed on both sides of the = sign.

2. spaces are required between commands and options.

3. Spaces on both sides of the MPs queue are optional.

Let's take a look at the various causes of errors caused by space:

1) when assigning values = There is a space on the left of the number

var ="abc"
The error message is:

Var: command not found

This is because when bash explains this command, var will be used as a command to find it in the system PATH. Once the var command cannot be found, the system prompts "The var command cannot be found", and subsequent statements cannot be executed.

2) When assigning values, there is a space on the right of the = sign.

var= "abc"

The error message is:

Abc: command not found

This is because there is such a command execution method in bash: var = string command

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

3) There are spaces around = During the value assignment operation.

This is the same as the first case.

4) No space is used between commands.

There are several common situations:

[If and [no space]

if[ -n "abc" ];then echo ok;fi

Cause of error: Many people think that [] is only a Boolean symbol in bash. In fact, [] is equivalent to test in bash. It is certainly a command.

If is an independent command, and [] is also an independent command. The two commands must have spaces. Otherwise, bash can only be understood as a whole in the case of if [. Therefore, an error is returned.

[Xxx] There is no space at both ends of the parameter]

if ["abc" = "abc" ];then echo ok;fi

Error cause: the same bash cannot understand what ["abc" is. Remember that [] is not just a symbol, but a command.

["Abc" = "CBA"] There is no space between the parameters]

if [ "abc"="abc" ];then echo ok;fi

Error cause:

= There are spaces on both sides. It is an expression. If the values on both sides are equal, a bool value is returned.

= If there is no space on both sides, bash will regard "abc" = "CBA" as a whole, and it will always return true.

As for the pipe, although | Spaces on both sides are dispensable, the spaces on both sides are generally retained for the sake of appearance.

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.