Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

4 Answers 4

up vote 1 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

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

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

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
fi

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

Your Answer

 
discard

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