I just wondered what exactly the difference between
[[ $STRING != foo ]]
and
[ $STRING != foo ]
is, apart from that the latter is posix-compliant, found in sh and the former is an extension found in bash.
|
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. Double Brackets i.e. In bash, for numeric comparison we use |
||||
|
|
ifstatement, see mywiki.wooledge.org/BashPitfalls#if_.5Bgrep_foo_myfile.5D – Kev Oct 15 '15 at 19:09