Shell built-in command test single-bracket difference

Source: Internet
Author: User

1, the single-brace test command to separate the word of the variable, when the value of the variable contains whitespace, the variable is enclosed in quotation marks, while the double-brace Test command does not separate the word from the variable.

The following cases are divided into a single word, a phrase containing spaces.

[Email protected] yujia]# echo $name

Hello

[Email protected] yujia]# [$name = "Hello"]

[[email protected] yujia]# echo $?

0

[[Email protected] yujia]# [[$name = "Hello"]]

[[email protected] yujia]# echo $?

0

[Email protected] yujia]#

Come to the conclusion that because the variable has only one word, the single-parenthesis word separation does not affect the variable.

[Email protected] yujia]# echo $name 1

Hello World

[[Email protected] yujia]# [$name 1 = "Hello World"]

-bash: [: Too many arguments

[[email protected] yujia]# echo $?

2

[[Email protected] yujia]# ["$name 1" = "Hello World"]

[[email protected] yujia]# echo $?

0

[[Email protected] yujia]# [[$name 1 = "Hello World"]

[[email protected] yujia]# echo $?

0

[Email protected] yujia]#

This time the variable is a phrase, with a space type. When we were going to test it, we found that there were too many bash response parameters, what's going on? It turns out that the single quote test command separates the word from the variable, and the result becomes

[Hello world = ' Hello World '], with that string hello, a comparison between the string world and the string Hello World. So if the variable in the single-parenthesis test command contains a space, but it also has to be compared to a string, it must be given a double quotation mark, and it will not have an error. In the following double-brace test command, even if the variable contains a space, it is not possible, because it cannot separate variable words.

2. The test command of the square brackets usually tests the value of the expression with the built-in test command, and the test command is also linked to the square brackets. In this way, you can either use a separate test command, or you can test the value of an expression by enclosing the expression in parentheses.

[[email protected] yujia]# LL ABC

-rw-r--r--1 root root 0 Sep 08:59 ABC

[Email protected] yujia]# TEST-R ABC; echo $?

0

[Email protected] yujia]# [-R ABC]; echo $?

0

[Email protected] yujia]#

3. The single-brace test command does not extend the shell metacharacters, and the double-brace Test command expands the shell metacharacters.

[Email protected] yujia]# Name=tom

[Email protected] yujia]# [$name = t??]

[[email protected] yujia]# echo $?

1

[[Email protected] yujia]# [[$name = T??]]

[[email protected] yujia]# echo $?

0

[Email protected] yujia]#

4. In the double-brace Test command, if a string (with or without a space) is just a normal string in the expression, not part of a pattern, it must also be enclosed in quotation marks.

My understanding is that if a string value (the one on the right) is not enclosed in double quotes, the string is a pattern, and if it contains shell metacharacters, bash expands it. If the string is double-quoted, it is a very ordinary string, even if the string contains special characters, that is, when the ordinary content to deal with.

[Email protected] yujia]# echo $name

Tom

[Email protected] yujia]# echo $name 1

Tomm

[[Email protected] yujia]# [[$name = Tom]]

[[email protected] yujia]# echo $?

0

[[Email protected] yujia]# [[$name = Tom?]

[[email protected] yujia]# echo $?

1

[[Email protected] yujia]# [[$name 1 = Tom]]

[[email protected] yujia]# echo $?

1

[[Email protected] yujia]# [[$name 1 = Tom?]

[[email protected] yujia]# echo $?

0

[Email protected] yujia]#

[[Email protected] yujia]# [[$name = "Tom"]]

[[email protected] yujia]# echo $?

0

[[Email protected] yujia]# [[$name = "Tom?]]

[[email protected] yujia]# echo $?

1

[[Email protected] yujia]# [[$name 1 = "Tom"]]

[[email protected] yujia]# echo $?

1

[[Email protected] yujia]# [[$name 1 = "Tom?"]

[[email protected] yujia]# echo $?

1

5, [Express1–a Express2] This is placed in the single-bracket test command, because the single does not support the meta-character extension, so it can only be called expressions, they can be combined to form a logical test, but with or non-use (-a–o!) The form.

[[email protected] yujia]# ll mm

-r--r--r--2 root root 644 Sep 08:48 mm

[[Email protected] yujia]# [-R mm-a x mm]

[[email protected] yujia]# echo $?

1

[Email protected] yujia]#

[[Pattern1–a Pattern1]] This is placed in the double-brace Test command, because a single formula can support the meta-word Fuma, so it can be called pattern expression, it can be used for compound logic, but with or non-use (&& | | !) in the form of.

[Email protected] yujia]# echo $name
Tom
[[Email protected] yujia]# [[$name = [Tt]om && $name = t??]]
[[email protected] yujia]# echo $?
0
[Email protected] yujia]#

Shell built-in command test single-bracket difference

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.