EN
Bash - check if string is greater than (ASCII alphabetical order)
0 points
In this article, we would like to show you how to check if check if string is greater than another string in Bash.
In this example, we use escaped >
operator to check if string is greater than another string (in ASCII alphabetical order) inside the if
statement.
script.sh
file:
xxxxxxxxxx
1
2
3
string="bbb"
4
5
if [ "$string" \> "aaa" ]; then
6
echo "The $string is greater than aaa"
7
fi
Note: this approach should be used in POSIX shells where other solutions may not work.
Run in command line:
xxxxxxxxxx
1
./script.sh
Output:
xxxxxxxxxx
1
The bbb is greater than aaa
xxxxxxxxxx
1
2
3
string="bbb"
4
5
if test "$string" \> "aaa"; then
6
echo "The $string is greater than aaa"
7
fi
Note:
[ ]
notation is shorthand oftest
command.
xxxxxxxxxx
1
2
3
string="bbb"
4
5
if [[ "$string" > "aaa" ]]; then
6
echo "The $string is greater than aaa"
7
fi
Note:
[[ ]]
notation is like extendedtest
command, adding additional features (e.g. regular expressions).