106

I want to have some groups of conditions in a Bash if statement. Specifically, I'm looking for something like the following:

if <myCondition1 and myCondition2> or <myCondition3 and myCondition4> then...

How may I group the conditions together in the way I describe for use with one if statement in Bash?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
d3pd
  • 7,935
  • 24
  • 76
  • 127
  • I did but was unable to find the grouping trick (and didn't know the terminology to search in order to find it). When in doubt, ask. Thanks for the link! – d3pd Feb 19 '13 at 19:01
  • 8
    I'm not sure this should be marked as a duplicate. The answer to this question specifically addresses grouping conditions, whereas the answer to the duplicate does not. – JoBu1324 Feb 20 '14 at 17:44
  • 1
    I'm always amazed how easily questions can be marked as duplicates when they are clearly not. This question is about grouping conditions. There is nothing in the answers of that other question that actually answers this one. – Calimo Oct 05 '16 at 08:56
  • 1
    Related: [Compound if statements with multiple expressions in Bash](https://stackoverflow.com/q/11267569/6862601) – codeforester Aug 15 '19 at 20:10

1 Answers1

223

Use the && (and) and || (or) operators:

if [[ expression ]] && [[ expression ]] || [[ expression ]] ; then

They can also be used within a single [[ ]]:

if [[ expression && expression || expression ]] ; then

And, finally, you can group them to ensure order of evaluation:

if [[ expression && ( expression || expression ) ]] ; then
William
  • 4,787
  • 2
  • 15
  • 14
  • 8
    IMO this is a clearer answer than the "duplicate". In the third example, make sure to use double brackets - `[[ ]]` – JoBu1324 Feb 20 '14 at 17:58
  • 4
    For those who care about compatibility with other shell types, I'll note that doing the above does not appear to work with single brackets `[]`. – Seldom 'Where's Monica' Needy Jul 07 '16 at 02:50
  • 39
    Be aware that in the first example, the `&&` and `||` are the shell's pipeline operators. In the second and third examples, they are operators handled by the `[[...]]` conditional command parser. The first works on exit values of zero and non-zero, the rest work on return values of true and false. While all of the examples _work_, they work for different reasons. – Ti Strga Mar 13 '17 at 17:29
  • 1
    this question answers me. I wanted to know if it was possible to group using braces, then add more conditions after the group. This demonstrates that use https://unix.stackexchange.com/questions/290146/multiple-logical-operators-a-b-c-and-syntax-error-near-unexpected-t . – blamb Sep 21 '18 at 22:27
  • and how can you break down a long line (e.g. more than 80 chars) of several conditions? Line-continuation `\` and then newline after a logical operator where it makes sense to? – tarabyte Aug 04 '21 at 17:47
  • @tarabyte You can end the line with a backslash `\` and continue on the next line. It doesn't matter if the backslash comes before or after a logical operator. – Dave Yarwood Jan 16 '22 at 03:22