whats the best practice for conditionals in bash when checking multiple statements?
bash
Solution
Put `&&`/`||` inside brackets for `[[ ]]`. Outside is also accepted.
Put `&&`/`||` outside brackets for `[ ]`. Inside is NOT allowed.
This is due to the fact that `&&` binds normal commands together based on return value, e.g.
wget file && echo "Success"
`[`, despite its funny name, is a regular command and obeys the same rules as e.g. `wget` or `echo`.
`[ foo || bar ]` is two commands, `[ foo` and `bar ]`, neither of which are valid.
`[[ .. ]]` on the other hand is not a normal command but special shell syntax. `[[ foo || bar ]]` is a single command, and interpretted accordingly.
Problem
Given your trying to check that a variable is not empty and not some other value as in the following code: ``` if [ ! -z "$foo" ] && [[ ${foo} != "bar" ]]; then ``` what is the best practice for accomplishing this. I've seen bash conditionals written several ways including the following... ``` if [[ ! -z "$foo" && ${foo} != "bar" ]]; then ``` I understand there is a difference when using the single brackets and the double, I'm more concerned with when to put the `&&` or `||` inside the brackets or out.