C program return codes and && bash symbol?

后端 未结 3 1459
清酒与你
清酒与你 2021-01-19 10:40

In bash, we can use the && operator to execute two commands. For example:

./foo && ./bar

Will first execute foo

相关标签:
3条回答
  • 2021-01-19 11:17

    The C language convention that 0 is false and anything else true, is just that, a convention. Bash (and unix shells in general), use the opposite convention: 0 is true, anything else is false.

    $ if ( exit 0 ); then echo true; else echo false; fi
    true
    $ if ( exit 1 ); then echo true; else echo false; fi
    false
    $ if ( exit 2 ); then echo true; else echo false; fi
    false
    

    Because of this, the true command always exits with a status of 0, while false exits with a status of 1.

    $ true; echo $?
    0
    $ false; echo $?
    1
    

    This can be rather disconcerting for someone who's used to the C convention, but it makes a lot more sense in shell terms that truth=success=zero exit status, while false=failure=nonxero exit status.

    0 讨论(0)
  • 2021-01-19 11:23

    You're not missing anything. You just have to keep in mind that true and false aren't fundamental concepts in the shell. success and failure are.

    0 讨论(0)
  • 2021-01-19 11:27

    You are missing that Bash's && operator is defined thusly:

    command1 && command2
    command2 is executed if, and only if, command1 returns an exit status of zero.

    0 讨论(0)
提交回复
热议问题