13

At this question in the answer from Kyle Brandt the [[ ]] construct is described as a "bash built-in test command". The following is shown as an example.

 if [[ ( $a -gt 2 ) && ( $a -lt 5 ) ]]; then ... 

Why are double square brackets, literally [[ ]], necessary?

0

2 Answers 2

10

[[ is a much more versatile version of [ in bash.

For example, Using the [[ ... ]] test construct, rather than [ ... ] can prevent many logic errors in scripts. For example, the &&, ||, <, and > operators work within a [[ ]] test, despite giving an error within a [ ] construct.

also, their implementations are different

-bash-3.2$ type [ [ is a shell builtin -bash-3.2$ type [[ [[ is a shell keyword 

Refer here for a good explanation

Sign up to request clarification or add additional context in comments.

1 Comment

It's worse than that; [ "$this" > "$that" ] won't give an error, it'll just do something completely different from what you expected. (Specifically: it'll redirect output to the file specified by $this, then test whether $that is nonblank.)
3

[[ ]] are equal to test keyword.

You can also use [ ] but [[ ]] isn't compatible with all shell types

To me, if I understand the real sense of question, question itself isn't well-formulated.
Those brackets have to be here not so much for order determination, but because the bash synopsis require them

Edit

Question is changed, so my answer too.

[[ ]] is used against [ ] because you don't have to worry about quoting the left hand side of the test that will be read as a variable.
Moreover, < and > doesn't need to be escaped

6 Comments

If I can use [ ], are you saying there is never a reason to use [[ ]].
You're right, the order of operations has nothing to do with the question so I edited the question to remove any mention of ( ) determining the order of operations.
@broiyan: if you haven't to worry about being portable, someone supports that [[ ]] is better because you don't have to worry about quoting the left hand side of the test will be read as a variable. Moreover, < and > doesn't need to be escaped
@broiyan: take a look at my update
A quick scroll through this link wiki.bash-hackers.org/commands/classictest does not seem to turn up any information on why one would double the square brackets. I don't know what you mean by the "left hand side" of the test.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.