-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
2
  • 1
    "But when i run the script just giving the error.", what error? Commented Jan 16, 2023 at 20:44
  • if (( a > 8 )) Commented Jan 16, 2023 at 20:45

1 Answer 1

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))
Sign up to request clarification or add additional context in comments.

2 Comments

Not my downvote, but some users downvote correct answers to obvious duplicates to discourage answering them.
All clear thanks for the assist...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.