Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I can only do simply expressions like 1=1 and 0=1

test 1 = 1 -a 0 = 1 

But how to do complex expressions like 1=1 and (0=1 or 1=1) ?

share|improve this question
up vote 2 down vote accepted

Either use multiple test calls:

test 1 = 1 && test 0 = 1 -o 1 = 1

or [[:

[[ 1 = 1 && (0 = 1 || 1 = 1) ]]
share|improve this answer

You can use test or [ ... ] as long as you are careful, remembering that parentheses have to be escaped and terms have to be separated by spaces:

if [ \( a = a \) -a \( a = b -o b = b \) ]
then echo Yes
else echo No

That echoes 'Yes' for me. It is usually easier, though, to use the Bash and Korn shell [[ ... ]] operator instead; you don't have to do as much escaping.

share|improve this answer

The question is already answered, but a couple of style tips you might consider (and maybe reject :-) ):

If you are the one writing it, and find it confusing, then maybe:

  • Use extra () even if they are not needed, might make it clearer.
  • Also maybe use nested loops instead of lots of and statements.
share|improve this answer

Florian is mostly correct above, but keep in mind that bash also has arithmetic expansion which is a bit nicer when you are comparing variables.

See here for more info.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.