Interview Prep/Shell Scripting/Arrays, conditionals and loops

What is the difference between `[ ]`, `[[ ]]` and `(( ))`?

BeginnerConceptArrays, conditionals and loops
Answer
  • [ ] is the test command (POSIX). Arguments undergo word splitting and globbing, so you must quote; use -a/-o sparingly (deprecated) and = for string equality.
  • [[ ]] is a Bash keyword: no word splitting or globbing on variables, supports &&, ||, <, > (string compare), pattern matching with == and regex with =~.
  • (( )) is arithmetic: (( a > b )), (( n % 2 == 0 )).
[ -n "$var" ] && [ "$a" = "$b" ]     # POSIX sh
[[ $file == *.tar.gz && -s $file ]]   # Bash
(( disk_pct >= 90 ))                  # numbers

In Bash scripts use [[ ]] for strings and files, (( )) for numbers.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Arrays, conditionals and loops