Jay Taylor's notes

back to listing index

What is the difference between double and single square brackets in bash?

[web search]
Original source (serverfault.com)
Tags: bash shell-scripting [ [[ serverfault.com
Clipped on: 2016-05-05

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.

asked Aug 9 '09 at 21:11
Image (Asset 2/15) alt=
0x89
1,72121312
   upvote
  flag
   upvote
  flag
In case you were also wondering about using no brackets at all, e.g. in the context of an if statement, see mywiki.wooledge.org/BashPitfalls#if_.5Bgrep_foo_myfile.5D – Kev Oct 15 '15 at 19:09
up vote 142 down vote accepted

There are several differences. In my opinion, a few of the most important are:

  1. [ is a builtin in Bash and many other modern shells. The builtin [ is similar to test with the additional requirement of a closing ]. The builtins [ and test imitate the functionality /bin/[ and /bin/test along with their limitations so that scripts would be backwards compatible. The original executables still exist mostly for POSIX compliance and backwards compatibility. Running the command type [ in Bash indicates that [ is interpreted as a builtin by default. (Note: which [ only looks for executables on the PATH and is equivalent to type -P [)
  2. [[ is not as compatible, it generally won't work with whatever /bin/sh points to. So [[ is the more modern bash / zsh / korn option.
  3. Because [[ is built in to the shell and does not have legacy requirements, you don't need to worry about word splitting based on the IFS variable to mess up on variables that evaluate to a string with spaces. Therefore, you don't really need to put the variable in double quotes.

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 [[ is a better choice if you don't need to worry about being as portable. The main reasons are:

  1. You don't have to worry about quoting the left hand side of the test so that it actually gets read as a variable.
  2. You don't have to escape less than and greater than < > with backslashes in order for them not to get evaluated as input redirection, which can really mess some stuff up by over writing files. This again goes back to [[ being a builtin. If [ (test) is an external program the shell would have to make an exception in the way it evaluates < and > only if /bin/test is being called, which wouldn't really make sense.
answered Aug 9 '09 at 21:56
Image (Asset 4/15) alt=
Kyle Brandt
55.7k40203372
3 upvote
  flag
Thanks, the link to the bash FAQ was what I was looking for (did not know about that page, thanks). – 0x89 Aug 9 '09 at 22:07
1 upvote
  flag
I edited your post with this information, but [ and test are executed as builtins. The builtins were designed to replace /bin/[ and /bin/test but needed to reproduce the limitations of the binaries too. The command 'type [' verifies that the builtin is used. 'which [' only searches for executables on the PATH and is equivalent to 'type -P [' – klynch Jul 18 '11 at 22:55

In Short:

[ ] are bash Builtins

[[ ]] are bash Keywords

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:

  $ [ a < b ]
 -bash: b: No such file or directory
  $ [[ a < b ]]

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

answered Dec 29 '10 at 19:42
Image (Asset 5/15) alt=
abhiomkar
64357

Some differences on Bash 4.3.11:

  • POSIX vs Bash extension:

  • regular command vs magic

    • [ is just a regular command with a weird name.

      ] is just an argument of [ that prevents further arguments from being used.

      Nothing is altered in the way that Bash parses the command.

      In particular, < is redirection, && and || concatenate multiple commands, ( ) generates subshells unless escaped by \, and word expansion happens as usual.

    • [[ X ]] is a single construct that makes X be parsed magically. <, &&, || and () are treated specially, and word splitting rules are different.

      There are also further differences like = and =~.

  • <

    • [[ a < b ]]: lexicographical comparison
    • [ a \< b ]: Same as above. \ required or else does redirection like for any other command. Bash extension.
  • && and ||

    • [[ a = a && b = b ]]: true, logical and
    • [ a = a && b = b ]: syntax error, && parsed as an AND command separator cmd1 && cmd2
    • [ a = a -a b = b ]: equivalent, but deprecated by POSIX
    • [ a = a ] && [ b = b ]: POSIX recommendation
  • (

    • [[ (a = a || a = b) && a = b ]]: false
    • [ ( a = a ) ]: syntax error, () is interpreted as a subshell
    • [ \( a = a -o a = b \) -a a = b ]: equivalent, but () is deprecated by POSIX
    • ([ a = a ] || [ a = b ]) && [ a = b ] POSIX recommendation
  • word splitting

    • x='a b'; [[ $x = 'a b' ]]: true, quotes not needed
    • x='a b'; [ $x = 'a b' ]: syntax error, expands to [ a b = 'a b' ]
    • x='a b'; [ "$x" = 'a b' ]: equivalent
  • =

    • [[ ab = a? ]]: true, because it does pattern matching (* ? [ are magic). Does not glob expand to files in current directory.
    • [ ab = a? ]: a? glob expands. So may be true or false depending on the files in the current directory.
    • [ ab = a\? ]: false, not glob expansion
    • = and == are the same in both [ and [[, but == is a Bash extension.
    • printf 'ab' | grep -Eq 'a.': POSIX ERE equivalent
    • [[ ab =~ 'ab?' ]]: false, loses magic with ''
    • [[ ab? =~ 'ab?' ]]: true
  • =~

    • [[ ab =~ ab? ]]: true, POSIX extended regular expression match, ? does not glob expand
    • [ a =~ a ]: syntax error. No bash equivalent.
    • printf 'ab' | grep -Eq 'ab?': POSIX equivalent

Recommendation: always use [].

There are POSIX equivalents for every [[ ]] construct I've seen.

If you use [[ ]] you:

  • lose portability
  • force the reader to learn the intricacies of another bash extension. [ is just a regular command with a weird name, no special semantics are involved.
answered Jul 12 '15 at 10:22
Image (Asset 6/15) alt=

Based on a quick reading of the relevant sections of the manpage, the primary difference appears to be that the == and != operators match against a pattern, rather than a literal string, and also that there's the =~ regex comparison operator.

answered Aug 9 '09 at 21:17
Image (Asset 7/15) alt=
womble
74k9112178

Single Bracket i.e. []
For comparison ==, !=, <, and > and should be used and for numeric comparison eq, ne,lt and gt should be used.

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 ==, !=, <, and > can use literally.

  • [ is a synonym for test command. Even if it is built in to the shell it creates a new process.
  • [[ is a new improved version of it, which is a keyword, not a program.
  • [[ is understood by Korn and Bash.

Source

answered Feb 8 at 3:15
Image (Asset 8/15) alt=

Your Answer

asked

6 years ago

viewed

70810 times

active

2 months ago

Hot Network Questions

Technology Life / Arts Culture / Recreation Science Other
  1. Stack Overflow
  2. Server Fault
  3. Super User
  4. Web Applications
  5. Ask Ubuntu
  6. Webmasters
  7. Game Development
  8. TeX - LaTeX
  1. Programmers
  2. Unix & Linux
  3. Ask Different (Apple)
  4. WordPress Development
  5. Geographic Information Systems
  6. Electrical Engineering
  7. Android Enthusiasts
  8. Information Security
  1. Database Administrators
  2. Drupal Answers
  3. SharePoint
  4. User Experience
  5. Mathematica
  6. Salesforce
  7. ExpressionEngine® Answers
  8. more (13)
  1. Photography
  2. Science Fiction & Fantasy
  3. Graphic Design
  4. Movies & TV
  5. Seasoned Advice (cooking)
  6. Home Improvement
  7. Personal Finance & Money
  8. Academia
  9. more (9)
  1. English Language & Usage
  2. Skeptics
  3. Mi Yodeya (Judaism)
  4. Travel
  5. Christianity
  6. Arqade (gaming)
  7. Bicycles
  8. Role-playing Games
  9. more (21)
  1. Mathematics
  2. Cross Validated (stats)
  3. Theoretical Computer Science
  4. Physics
  5. MathOverflow
  6. Chemistry
  7. Biology
  8. more (5)
  1. Stack Apps
  2. Meta Stack Exchange
  3. Area 51
  4. Stack Overflow Careers
site design / logo © 2016 Stack Exchange Inc; user contributions licensed under cc by-sa 3.0 with attribution required
rev 2016.5.5.3546