Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

This guide says:

 An if/then construct tests whether the exit status of a list of commands is 0

If I execute 0 in the bash, it says:

0: command not found

So its exit status is not 0, and [ 0 ] should return a non-zero value exit status too. But it returns 0 exit status actually, and if [ 0 ] executes the then statement, not the else statement.
Can anyone explain it?

share|improve this question
1  
if[0] will not even be recognized as an if; it will try to find a command called if[0], typically fail, and tell you if[0]: command not found. Assuming you're getting 0: command not found, are you sure there aren't spaces you're neglecting to include, in your statement of the problem here? Please note that if[0], if [0], and if [ 0 ] are all quite different. –  Eliah Kagan Mar 13 '13 at 12:17
    
@Eliah Kagan Oh, so ti is. Sorry for my typo. –  acgtyrant Mar 14 '13 at 8:33

1 Answer 1

up vote 5 down vote accepted

[ isn't part of the if statement, it is a command that evaluates expressions. [ 0 ] returns true bacause the expression evaluates to 0. If you try if [ 1 = 2 ] the then statement will not run.

Check the manual page for more info.

share|improve this answer
    
I will read this manual page, thank you! –  acgtyrant Mar 14 '13 at 8:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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