Not equal to in Unix

Guys,

I am trying to do below operation

LAST_TRANSACTION=2
if [[ $LAST_TRANSACTION != 1 || $LAST_TRANSACTION != 2 || $LAST_TRANSACTION != 3 ]]; then
	# do something
fi

If the LAST_TRANSACTION variable is not equal to 1 or 2 or 3 then code inside the if block should be execute.

This code is not working, Any help is appreciated.

LAST_TRANSACTION=2 if [[ $LAST_TRANSACTION -ne 1 -o $LAST_TRANSACTION -ne 2 -o $LAST_TRANSACTION -ne 3 ]]; then # do something fi

To compare strings use "=" for equal and "!=" for not equal.

To compare numbers use "-eq" for equal "-ne" for not equal.

Still I get a problem

I am using below code, what ever might be the LAST_TRANSACTION value, code inside the if block is execute.

LAST_TRANSACTION=2
if [[ $LAST_TRANSACTION -ne 1 || $LAST_TRANSACTION -ne 2 || $LAST_TRANSACTION -ne 3 ]]; then
echo hi
fi

Since its a or condition and always it will come to If condition :slight_smile: change it to and condition and try

Dude its an or condition so whatever may be the values it will go to if condition. Go for and condition

if [[ $LAST_TRANSACTION -ne 1 && $LAST_TRANSACTION -ne 2 && $LAST_TRANSACTION -ne 3 ]]; then

or

if ! [[ $LAST_TRANSACTION -eq 1 || $LAST_TRANSACTION -eq 2 || $LAST_TRANSACTION -eq 3 ]]; then

you have used OR '||' operator instead of AND '&&'..so when it is checking the first condition that $LAST_TRANSACTION is not eqal to 1, it gets true and condition inside IF loop gets executed. :):slight_smile: