Frontier Software

Logic

Whenever you’re making a Bash script, you should always use [[ rather than [. — BashGuide

Predicates (aka boolean functions, conditionals, tests…) are generally within double square brackets [[...]] for text or double round brackets ((...)) for arithmetic. Historically they were within single brackets, and the double bracket notation comes from the Korn shell.

A key advantage of double square brackets is they support regex patterns using string =~ regex, and confusingly the syntax isn’t the same as Bash globbing. The manual has the details in the conditiional constructs section.

Whereas double quoting left-hand-side variables was required by [ and isn’t assuming there are no spaces in [[, it’s a good habit to double quote both sides.

Both the single and double bracketed tests support numerous flags to check if files exist, are empty and the same for strings and variables. The manual contains the full list in its conditional expressions section.

Pitfalls with [[...]]

An if can test any command, not just conditions enclosed within brackets. — Test Constructs of Advanced Bash-Scripting Guide.

if…then is just and

Bash is prologish in that the return status of functions can often be used navigate without if predicate; then ... fi:

if [[ -z $var ]]; then continue; fi

can be simplified to:

[[ -z $var ]] && continue