Answer the question
In order to leave comments, you need to log in
How to compare 2 numbers in bash?
i can compare 2 numbers like:
if [ $a -eq $b ]
but why do i get all sorts of errors if i want something like this if (a+1 == b)
if [ $(($a+ 1 )) -eq $b]
if [ $a + 1-eq $b ]
if ["$a" + 1 -eq $b]
how does it even work
Answer the question
In order to leave comments, you need to log in
To perform arithmetic operations, you can use the let statement (built into bash):
$ let "a=2+2"
$ echo $a
4
$ if (( $a > 2 )); then echo "Bigger"; else echo "smaller"; fi
Bigger
$ if (( $a+2 > 6 )); then echo "Bigger"; else echo "smaller or equal"; fi
smaller or equal
[[email protected] ~]$ a=1
[[email protected] ~]$ b=2
[[email protected] ~]$ c=3
[[email protected] ~]$
[[email protected] ~]$ [ $((a + b)) -eq $c ]
[[email protected] ~]$ echo $?
0
[[email protected] ~]$ [ $((a + b)) -lt $c ]
[[email protected] ~]$ echo $?
1
[[email protected] ~]$ [ $((a + b - 1)) -lt $c ]
[[email protected] ~]$ echo $?
0
[[email protected] ~]$ [ $((a + b)) -gt $c ]
[[email protected] ~]$ echo $?
1
[[email protected] ~]$ [ $((a + b + 1)) -gt $c ]
[[email protected] ~]$ echo $?
0
[[email protected] ~]$
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question