\<to prevent it redirecting (or switch to
[[ .. ]]).
if [ "aardvark" < "zebra" ] then echo "Alphabetical!" fi
if [ "aardvark" \< "zebra" ] then echo "Alphabetical!" fi
or optionally in Bash/Ksh:
if [[ "aardvark" < "zebra" ]] then echo "Alphabetical!" fi
You are using the operator
> in a
[ test expression.
In this context, it will be considered a file redirection operator instead, so
[ "aardvark" < "zebra" ] is equivalent to
[ "aardvark" ] < ./zebra, which is true if there exists a readable file
zebra in the current directory.
If you wanted to compare two strings lexicographically (alphabetically), escape the
> with a backslash as in the correct example.
If you want to compare two numbers numerically, use
ShellCheck is a static analysis tool for shell scripts. This page is part of its documentation.