1

When I ran the following script

if [[ 10 -gt 2 ]]
then
    echo "10 is greater than 2"
else
    echo "10 is less than 2"
fi

it output the expected result

10 is greater than 2

However, when I ran the following one

if [[ 10 > 2 ]]
then
    echo "10 is greater than 2"
else
    echo "10 is less than 2"
fi

it output the following result

10 is less than 2

It seems like it's performing String comparison in this case. What are the logics behind these two?

1 Answers1

3

As you have guessed, > performs string comparison and -gt performs numeric comparison. This is documented in the Bash Reference Manual, §6.4 "Bash Conditional Expressions".

ruakh
  • 175,680
  • 26
  • 273
  • 307
  • The `>` string comparison operator is also mentioned in the [POSIX standard for the `test` / `[ ]` command](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/test.html), although it's not actually part of the standard; but if your `test` / `[ ]` command supports it, it should have the same semantics. One difference from `[[ ]]`, though: `test` and `[ ]` are normal commands, so `>` will be parsed as an output redirect unless it's quoted or escaped. – Gordon Davisson Jul 22 '19 at 00:19