-3

I am dealing with a bash script in Ubuntu 21.10. Simple code is below the text. Looks like easy. But when i run the script just giving the error. What would be the mistake inside the code or in other way could not analyzed in scripting. Could you assist. Just stuck in the script before action if this is an internal error or different kind of unknown parameter.

for a in {1..10};do
if [[ (($a>8)) ]]
then
    echo "over"
else
    echo "$a"
fi
done

output is:
1
2
3
4
5
6
7
8
over
10
synapsis
  • 15
  • 2

1 Answers1

2

Within [[ ... ]], parentheses are used for grouping; your condition is equivalent to [[ $a > 8 ]] (no word splitting here, so you don't need blanks around >).

And in [[ ... ]], </> are lexicographic comparisons; that's why 10 is considered less than 8.

There are two ways to fix this:

  • Use the correct comparison operator: [[ $a -gt 8 ]]
  • Use the arithmetic conditional: ((a > 8))
Benjamin W.
  • 46,058
  • 19
  • 106
  • 116