-3
if [[ 6 > 50 ]]; then
    echo "true"
fi

$ bash script.sh

I'm missing something very obvious here. Why is 6 greater than 50 ??

** EDIT **

I'm also try to solve for

if [[ 6.5 > 50 ]]; then
    echo "true"
fi
Jacksonkr
  • 201

3 Answers3

3

If you need to compare floats, the easiest way is to call out to an external tool like awk or bc

a=6.1
b=50
if [[ "$(echo "$a > $b" | bc)" -eq 1 ]]; then echo "a greater than b"; fi
glenn jackman
  • 85,964
3

If you're comparing integers then use

if [[ 6 -gt 50 ]]; then echo "true"; fi

otherwise since bash cannot handle floating point

if (( $(echo "6.5 > 50" | bc -l) )); then echo "true"; fi

boots
  • 61
2

You supplied [[ args ]] which is a conditional expression, when you meant to perform arithmetic evaluation which uses the (( condition )) syntax.

chahu418
  • 131
  • 4