How to compare shell script?

How to compare shell script?

Or, why does the following script not print anything?

x=1 if[ $x = 1 ] then echo "ok" else echo "no" fi 
+7
source share
3 answers

With numbers, use -eq , -ne , ... for equals, not equals, ...

 x=1 if [ $x -eq 1 ] then echo "ok" else echo "no" fi 

And for others, use == not = .

+8
source

Short solution with AND and OR labels:

 x=1 (( $x == 1 )) && echo "ok" || echo "no" 
+4
source

It depends on the language. With bash, you can use the == operator. Otherwise, you can use -eq -lt -gt for equal, lower, more than.

 $ x=1 $ if [ "$x" == "2" ]; then echo "yes"; else echo "no"; fi no 

Edit: Added spaces arround == and tested with 2.

+1
source

All Articles