Jon Abraham
Jon Abraham

Reputation: 955

Conditional statement in bash

x=23; y=223; [[ $x < $y ]] &&
echo yes || echo no;

Can someone explain me why am i getting answer as no instead of yes. I am new to bash. I would appreciate if someone could explain this.

Upvotes: 1

Views: 70

Answers (1)

anubhava
anubhava

Reputation: 784938

Use -lt to compare numeric comparison:

x=23; y=223; [[ $x -lt $y ]] && echo yes || echo no;
yes

< or > or == operators do string comparison where 23 is * lexicographically* not grater that 223 hence giving no output.

Or better ((...)) arithmetic evaluator in BASH:

x=23; y=223; (( x < y )) && echo yes || echo no
yes

Upvotes: 3

Related Questions