How do I get the effect and usefulness of “set -e” inside a shell function?

后端 未结 10 1887
北荒
北荒 2020-11-28 06:18

set -e (or a script starting with #!/bin/sh -e) is extremely useful to automatically bomb out if there is a problem. It saves me having to error ch

10条回答
  •  臣服心动
    2020-11-28 06:52

    From documentation of set -e:

    When this option is on, if a simple command fails for any of the reasons listed in Consequences of Shell Errors or returns an exit status value > 0, and is not part of the compound list following a while, until, or if keyword, and is not a part of an AND or OR list, and is not a pipeline preceded by the ! reserved word, then the shell shall immediately exit.

    In your case, false is a part of a pipeline preceded by ! and a part of if. So the solution is to rewrite your code so that it isn't.

    In other words, there's nothing special about functions here. Try:

    set -e
    ! { false; echo hi; }
    

提交回复
热议问题