0

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
7

> redirects to a file. The file will be created even if the former part of the redirection (i.e. 1 in your case) doesn't make sense.

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

  • 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 . – steeldriver 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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