336

I have a couple of variables and I want to check the following condition (written out in words, then my failed attempt at bash scripting):

if varA EQUALS 1 AND ( varB EQUALS "t1" OR varB EQUALS "t2" ) then 

do something

done.

And in my failed attempt, I came up with:

if (($varA == 1)) && ( (($varB == "t1")) || (($varC == "t2")) ); 
  then
    scale=0.05
  fi

5 Answers 5

846

What you've written actually almost works (it would work if all the variables were numbers), but it's not an idiomatic way at all.

  • (…) parentheses indicate a subshell. What's inside them isn't an expression like in many other languages. It's a list of commands (just like outside parentheses). These commands are executed in a separate subprocess, so any redirection, assignment, etc. performed inside the parentheses has no effect outside the parentheses.
    • With a leading dollar sign, $(…) is a command substitution: there is a command inside the parentheses, and the output from the command is used as part of the command line (after extra expansions unless the substitution is between double quotes, but that's another story).
  • { … } braces are like parentheses in that they group commands, but they only influence parsing, not grouping. The program x=2; { x=4; }; echo $x prints 4, whereas x=2; (x=4); echo $x prints 2. (Also braces require spaces around them and a semicolon before closing, whereas parentheses don't. That's just a syntax quirk.)
    • With a leading dollar sign, ${VAR} is a parameter expansion, expanding to the value of a variable, with possible extra transformations.
  • ((…)) double parentheses surround an arithmetic instruction, that is, a computation on integers, with a syntax resembling other programming languages. This syntax is mostly used for assignments and in conditionals.
    • The same syntax is used in arithmetic expressions $((…)), which expand to the integer value of the expression.
  • [[ … ]] double brackets surround conditional expressions. Conditional expressions are mostly built on operators such as -n $variable to test if a variable is empty and -e $file to test if a file exists. There are also string equality operators: "$string1" == "$string2" (beware that the right-hand side is a pattern, e.g. [[ $foo == a* ]] tests if $foo starts with a while [[ $foo == "a*" ]] tests if $foo is exactly a*), and the familiar !, && and || operators for negation, conjunction and disjunction as well as parentheses for grouping. Note that you need a space around each operator (e.g. [[ "$x" == "$y" ]], not [[ "$x"=="$y" ]]), and a space or a character like ; both inside and outside the brackets (e.g. [[ -n $foo ]], not [[-n $foo]]).
  • [ … ] single brackets are an alternate form of conditional expressions with more quirks (but older and more portable). Don't write any for now; start worrying about them when you find scripts that contain them.

This is the idiomatic way to write your test in bash:

if [[ $varA == 1 && ($varB == "t1" || $varC == "t2") ]]; then

If you need portability to other shells, this would be the way (note the additional quoting and the separate sets of brackets around each individual test, and the use of the traditional = operator rather than the ksh/bash/zsh == variant):

if [ "$varA" = 1 ] && { [ "$varB" = "t1" ] || [ "$varC" = "t2" ]; }; then
10
  • 17
    It's better to use == to differentiate the comparison from assigning a variable (which is also =) Jun 19, 2014 at 11:07
  • 3
    You could emphasize that single brackets have completely different semantics inside and outside of double brackets. (Because you start with explicitly pointing out the subshell semantics but then only as an aside mention the grouping semantics as part of conditional expressions. Was confusing to me for a second when I looked at your idiomatic example.) Aug 28, 2017 at 13:16
  • 6
    Oh, I meant single (round) parentheses, sorry for the confusion. The ones in [[ $varA = 1 && ($varB = "t1" || $varC = "t2") ]] do not start a sub process although the first bullet point explicitly says: "What's inside [parentheses] isn't an expression like in many other languages" -- but it certainly is here! That is probably obvious to the experienced bash wiz, but not even to me, immediately. The confusion can arise because single parentheses can be used in an if statement, just not in expressions inside double brackets. Aug 30, 2017 at 13:41
  • 7
    @protagonist == is not really “more idiomatic” than =. They have the same meaning, but == is a ksh variant also available in bash and zsh, whereas = is portable. There isn't really any advantage to using ==, and it doesn't work in plain sh. Sep 14, 2019 at 7:03
  • 3
    @WillSheppard There are already many other differences between comparison and assignment: comparison is inside brackets, assignment is not; comparison has spaces around the operator, assignment doesn't; assignment has a variable name on the left, comparison only rarely has something that looks like a variable name on the left and you can and should put quotes anyway. You can write == inside [[ … ]], if you want but = has the advantage of also working in [ … ], so I recommend not getting into the habit of using == at all. Sep 14, 2019 at 7:06
45

It is very close.

if [[ $varA -eq 1 ]] && [[ $varB == 't1' || $varC == 't2' ]];
  then
    scale=0.05
  fi

should work.

Breaking it down,

[[ $varA -eq 1 ]]

is an integer comparison, whereas

$varB == 't1'

is a string comparison. Otherwise, I am just grouping the comparisons correctly.

Double square brackets delimit a conditional expression. And, I find the following to be a good reading on the subject: "(IBM) Demystify test, [, [[, ((, and if-then-else"

1
  • Just to be sure: The quoting in 't1' is unnecessary, right? Because as opposed to arithmetic instructions in double parentheses, where t1 would be a variable, t1 in a conditional expression in double brackets is just a literal string. I.e., [[ $varB == 't1' ]] is exactly the same as [[ $varB == t1 ]], right? Aug 28, 2017 at 13:21
12

A very portable version (even to legacy Bourne shell):

if [ "$varA" = 1 -a \( "$varB" = "t1" -o "$varB" = "t2" \) ]
then    do-something
fi

This has the additional quality of running only one subprocess at most (which is the process [), whatever the shell flavor.

Replace = with -eq if variables contain numeric values, e.g.

  • 3 -eq 03 is true, but
  • 3 = 03 is false. (string comparison)
2
  • Honest question: why is portability to the legacy Bourne shell important? Which are the use cases for sh where bash does not fill the bill? Nov 30, 2022 at 14:20
  • 1
    @András: there are many flavors of the shell, all except csh inherit from Bourne shell, few are bash compatible. E. g. Openwrt shell is "ash" which misses many bash characteristics. ksh, dash are used a lot but are not fully bash compatible. Compatibility is an issue for the programmer, to avoid learning a new shell specifics every time they move to another job. Just my 2 cents though. Dec 2, 2022 at 18:43
9

Here is the code for the short version of the if-then-else statement:

( [ $a -eq 1 ] || [ $b -eq 2 ] ) && echo "ok" || echo "nok"

Pay attention to the following:

  1. || and && operands inside the if condition (i.e., between round parentheses) are logical operands (or/and)

  2. || and && operands outside the if condition mean then/else

Practically, the statement says:

if (a=1 or b=2) then "ok" else "nok"

1
  • Parenthesis ( ... ) creates a subshell. May want to use braces { ... } instead. Any state created in a subshell won't be visible in the caller. Mar 28, 2018 at 5:58
-1
if ([ $NUM1 == 1 ] || [ $NUM2 == 1 ]) && [ -z "$STR" ]
then
    echo STR is empty but should have a value.
fi
1
  • 1
    Can you please tell me what is the use of -z flag in bash ?
    – Mazhar MIK
    Feb 21, 2021 at 17:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.