What are the string and integer comparison operators in `[[ ]]` and `[ ]`?
BeginnerConceptArrays, conditionals and loops
Answer
Strings: == / = (equal), !=, < / > (sort order), -z (empty), -n (non-empty). Integers in [ ]/[[ ]]: -eq -ne -lt -le -gt -ge.
[[ $env == prod ]]
[[ -z ${TOKEN:-} ]] && die "TOKEN is empty"
[[ $count -gt 10 ]] # integer compare
[[ $count > 10 ]] # STRING compare: "9" > "10" is true!
(( count > 10 )) # clearest for numbers
Mixing them up is a classic bug: > inside [[ ]] compares lexicographically, and inside [ ] it is a redirection that creates a file named 10.
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?IntermediateWhat is the difference between `[ ]`, `[[ ]]` and `(( ))`?BeginnerWhy does `[ $var = "prod" ]` fail when `var` is empty or contains spaces?Intermediate