Issue
Hi I can't seem to find this anywhere. What does this forward slash> mean in an if statement in bash?
eg:
if [ "$count" \> 0 ]; then
echo hello
else
echo goodbye
fi
Solution
The characters <
and >
(among others) are special in shell scripts: they perform input/output redirection. So when you're trying to use them in a test expression like this, with their "normal meaning" of "less than" and "greater than", you need to escape them, preventing them from being treated specially by the shell.
It's similar to the way you might write
cat file\ name
to cat a file with a space in its name, or
cat it\'s
to cat a file with a single quote character in its name. (That is, normally, the space character is "special" in that it separates arguments, and the single quote character is special in that it quotes things, so to allow these characters to actually be used as part of a file name, you have to quote them, in this case using \
to turn off their special meaning.)
Quoting ends up being complicated in the Unix/Linux shells, because there are typically three different quote characters: "
, '
, and \
. They all work differently, and the rules (while they mostly make sense) are complicated enough that I'm not going to repeat them here.
Another way to write this, avoiding the ugly quoting, would be
if [ "$count" -gt 0 ]
And it turns out this is preferable for another reason. If you use -gt
, it will do the comparison based on the numeric values of $count
and 0
, which is presumably what you want here. If you use >
(that is, \>
), on the other hand, it will perform a string comparison. In this case, you'd probably get the same result, but in general, doing a string comparison when you meant to compare numerically can give crazy results. See Charles Duffy's comment for an example.
Answered By - Steve Summit