-2

I feel like this should be really simple but I can't get past this step.

$num = 5
if [$num > 2]; then echo greater; fi

the problem is, I keep getting [5: command not found.

Why is it not evaluating the if [ test ] block correctly? It's like the shell forgot about the if and just moved on to "hey, [5 > 2 does not look like a command, I can't find [5"... but [5 isn't a command, it's part of the if test block?

I have tried using different brackets and using -gt instead of >. The problem is bash doesn't actually ever do the test. For some reason it ignores if.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • I think you need to a space in between `[` and `]`, i.e. `if [ $num > 2 ]; then` ... – devang Sep 13 '16 at 21:17
  • That's exactly it. I just posted an answer I found too. I knew it would be something fiddly. Cheers! – averagescripter Sep 13 '16 at 21:18
  • To mark a question as solved, instead of editing the subject, please mark an answer as accepted (by clicking the green checkmark to its left). – ruakh Sep 13 '16 at 21:24
  • The thing that goes after `if` *is* a command. Could be any valid command (or even series of commands separated by line breaks or semicolons), it just happens that the `[` command (which is actually another name for the `test` command) is really common in `if` statements. But you can use any other command as the `if` test, and you can use `[` anyplace you'd use any other command. – Gordon Davisson Sep 14 '16 at 05:16

1 Answers1

0

Just managed to find the answer: if compare strings get a "command not found"-Error

really unexpected, there needs to be a space between the [ brackets and the variable.

I would never have guessed.

Community
  • 1
  • 1