As long as you're using bash
, and comparing text strings
, the [[..]]
test is safer and more flexible. You can use &&
inside them for and/or operators.
if [[ "$db1user" == "$db2user" && "$db1pass" == "$db2pass" && "$db1package" == "$db2package" ]]; then
echo "It's good"
echo "No bueno"
Note also that "==
" is not actually valid syntax in the old "[
" test, although bash accepts it. You should properly only use the single-character "=
" in single-bracket tests.
If you're comparing integers
, however, you should use ((..))
if (( num1 == num2 && num3 == num4 && num5 == num6 )); then
Where num1-num6 are variables containing integer values.
Another option, and perhaps safer, more portable, and more readable in the long run, would be to simply use separate nested if
Finally, remember, QUOTE ALL OF YOUR VARIABLE SUBSTITUTIONS
. You should never leave the quotes off a parameter expansion unless you explicitly want the resulting string to be word-split by the shell (globbing patterns are also expanded). This is a vitally important concept in scripting, so train yourself to do it correctly now. You can learn about the exceptions later.
This is particularly important when using the old "[
" single-bracket test command.
(And actually, one of the benefits of "[[
" is that it's one of the exceptions where quotes aren't needed, at least on the left hand side. The right hand side, however, does exhibit differing effects when using "==
" and "=~
". See the links I gave above for more.)