Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

3
  • 5
    Testing if $? is equal to 0 with an if statement is pointless, if expects a command and if said command returns 0, it runs the code in the block. so if true; then echo hello; fi will echo hello since the command true returned 0. Commented Feb 4, 2014 at 22:18
  • 2
    @llua It's not pointless. $? holds the status of the last pipeline, which is not the test ([) command in the if statement. The example is testing whether some command was successful. You can do the same with && and ||, but it can make long, unreadable lines compared to if [ $? -eq 0 ]. The same argument goes for if some command Commented Feb 5, 2014 at 17:03
  • 1
    @bonsaiviking I am well aware of what $? expands to, i am pointing out there is no point in the test being used; since if some command does the same thing with one statement versus having two separate statements. if the command is already long, adding three more characters wouldn't make the 'unreadable' terribly more 'unreadable'. Commented Feb 5, 2014 at 18:25