2

For some inexplicable reason, when I run the following code (minimalized, obviously) a file called "0" is created. Could somebody please explain why this happens, and how to avoid it? Thanks very much

#!/bin/bash
while [ 1 > 0 ] 
    do
        exit
    done
0
9

> 0 redirects to a file named 0. Note > something or similar syntax does not have to be at the end. The file will be created even if the command being redirected doesn't make sense. In your case the command is [ 1 ] and it's syntactically valid: it checks if 1 is a non-empty string.

You probably meant [ 1 -gt 0 ]. See help [ and help test | less.

4
  • You can still use \< and \> for string comparison.
    – choroba
    Dec 15 '16 at 23:23
  • Or use double brackets IE [[ 1 :operator: 0 ]] without the escapes where operator is > or <. Seems that >= and <= are invalid so the usual -ge and -le should be used in those cases.
    – Jim
    Dec 15 '16 at 23:30
  • 3
    @Jim although it's syntactically legal, it's a string comparison i.e. a test of lexical order (try [[ 2 > 11 ]] for example) - so not the same as -gt . Dec 16 '16 at 2:40
  • @steeldriver Ah good catch!! I wasn't aware of that. Thank you for the correction.
    – Jim
    Dec 16 '16 at 17:35

Not the answer you're looking for? Browse other questions tagged or ask your own question.