Compare Numbers in POSIX sh with test and -lt
Compare numbers in POSIX sh with [ "$a" -lt "$b" ]. A bare less-than sign is a redirection and creates a file, and a non-number makes test exit 2.
A comparison like [ "$a" < "$b" ] does not compare anything, and the script carries on as if it had.
a=9; b=10
[ "$a" -lt "$b" ] && echo "-lt: 9 is less than 10"
[ "$a" \< "$b" ] || echo "string compare: 9 sorts after 10"
[ "$a" -lt abc ]; echo "status with a non-number: $?"
[ "$a" < "$b" ]; echo "redirect status: $?"-lt: 9 is less than 10
string compare: 9 sorts after 10
sh: 4: [: Illegal number: abc
status with a non-number: 2
sh: 5: cannot open 10: No such file
redirect status: 2Inside , the operators -lt, -le, -gt, -ge, -eq and -ne compare integers. A bare < is parsed by the shell before test runs, as an input redirection from a file named after the second number, so the command either fails with a missing file or, with >, creates one. Escaping the sign makes it a string comparison, which puts 9 after 10. The operators are listed in the [test utility specification.
A value that is not an integer makes test exit with status 2, which a script that only checks for zero reads as false. Check the input first, or use $(( )) arithmetic, which treats an empty variable as 0. A greater-than sign written by mistake leaves an empty file named after the number in the current directory.