What is the difference between `[ ]`, `[[ ]]` and `(( ))`?
BeginnerConceptArrays, conditionals and loops
Answer
[ ]is thetestcommand (POSIX). Arguments undergo word splitting and globbing, so you must quote; use-a/-osparingly (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.
More on Arrays, conditionals and loops
How do you declare, populate and iterate an indexed array?BeginnerWhat is the difference between `"${arr[@]}"` and `"${arr[*]}"`?IntermediateHow do associative arrays work and what are they good for in DevOps scripts?IntermediateHow do you read a command's output into an array correctly?IntermediateWhy does `[ $var = "prod" ]` fail when `var` is empty or contains spaces?IntermediateList the most-used file test operators.Beginner