rgo1991
rgo1991

Reputation: 83

what does the condition "\>" mean in an if statement in bash?

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

Upvotes: 2

Views: 957

Answers (2)

AlexPixel
AlexPixel

Reputation: 439

  • This \>means GREATER THAN for STRINGS in ASCII ALPHABET
  • BASH has diferent meaning for backslash symbol \
  • in your example \> is a symbol, not two things like \ + >
  • For numbers you use > BUT ALSO take in consideration the [ ] for arithmetic operations you would use (( ))
  • A constructor in bash is with this symbol [[ ]] it's used to compare with these symbols inside double brackets
    • &&, ||, <, and >
    • IF YOU USE single brackets [ ] with THE ABOVE symbols, YOU GET ERRORS IN BASH SCRIPT, you need the double brackets.

Other Meanings of BACKSLASH

  • As mentioned before, in bash, you use forward slash / for PATH /home/user/Desktop/file1.txt but if your file NAME has spaces, you need to ESCAPE the blank space, and you do it with backslash \, example given

file name with spaces.txt

  /home/alex/Desktop/file\ name\ with\ spaces.txt  

file name with symbols -> [Stackoverflow]fileName.txt

 /home/alex/Desktop/\[Stackoverflow\]fileName.txt 

You need to escape each BRACKET [ ] LIKE this \[ \]

Resources

Bash Constructors | Bash operators | Ascii Table

Upvotes: 0

Steve Summit
Steve Summit

Reputation: 47952

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.

Upvotes: 1

Related Questions