Jay Taylor's notes
back to listing indexWhat is the difference between double and single square brackets in bash?
[web search]
|
|||||||||||||||
There are several differences. In my opinion, a few of the most important are:
For the most part, the rest is just some nicer syntax. To see more differences, I recommend this link to a FAQ answer: What is the difference between test, [ and [[ ?. In fact, if you are serious about bash scripting, I recommend reading the entire wiki, including the FAQ, Pitfalls, and Guide. The test section from the guide section explains these differences as well, and why the author(s) think
|
|||||||||||||||
|
In Short:
Keywords: Keywords are quite like builtins, but the main difference is that special parsing rules apply to them. For example, [ is a bash builtin, while [[ is a bash keyword. They are both used for testing stuff, but since [[ is a keyword rather than a builtin, it benefits from a few special parsing rules which make it a lot easier:
The first example returns an error because bash tries to redirect the file b to the command [ a ]. The second example actually does what you expect it to. The character < no longer has its special meaning of File Redirection operator. Source: http://mywiki.wooledge.org/BashGuide/CommandsAndArguments
|
|||
Some differences on Bash 4.3.11:
Recommendation: always use There are POSIX equivalents for every If you use
|
||||
Based on a quick reading of the relevant sections of the manpage, the primary difference appears to be that the
|
|||
Single Bracket i.e. Enhanced Brackets i.e. In all the above examples, we used only single brackets to enclose the conditional expression, but bash allows double brackets which serves as an enhanced version of the single-bracket syntax. For comparison
|
||||
Your Answer
asked |
6 years ago |
viewed |
70810 times |
active |
Technology | Life / Arts | Culture / Recreation | Science | Other | ||
---|---|---|---|---|---|---|