Skip to content

Instantly share code, notes, and snippets.

@rafaelmaeuer
Last active August 28, 2023 09:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rafaelmaeuer/e7688d271e0099774b3ccf576b3a0c34 to your computer and use it in GitHub Desktop.
Save rafaelmaeuer/e7688d271e0099774b3ccf576b3a0c34 to your computer and use it in GitHub Desktop.

The single bracket [ is actually an alias for the test command, it's not syntax.

One of the downsides (of many) of the single bracket is that if one or more of the operands it is trying to evaluate return an empty string, it will complain that it was expecting two operands (binary). This is why you see people do [ x$foo = x$blah ], the x guarantees that the operand will never evaluate to an empty string.

The double bracket [[ ]], on the other hand, is syntax and is much more capable than [ ]. As you found out, it does not have the "missing operand" issue and it also allows for more C-like syntax with >, <, >=, <=, !=, ==, &&, || operators.

My recommendation is the following: If your interpreter is #!/bin/bash, then always use [[ ]]

It is important to note that [[ ]] is not supported by all POSIX shells, however many shells do support it such as zsh and ksh in addition to bash

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment