4 ms·
`set -e` has a couple of surprising corner cases and in the details is pretty hard to understand. The documentation in `man bash` for the `set -e` flag is 28 li
by matvore 5y ago
`set -e` has a couple of surprising corner cases and in the details is pretty hard to understand. The documentation in `man bash` for the `set -e` flag is 28 lines in my terminal, and the other flags are 2 or 3 lines.
One such corner case is in pipelines. Only the last command in a pipeline can cause the script to terminate.
Another corner case is `foo && bar`, often used as an abbreviated `if`, will not exit when `foo` fails.
It is not a significant task to just add `|| exit $?` after any command whose failure should cause an abort.
- mgerdts 5y agoMy scripts tend to start with set -euo pipefail This addresses the pipeline case you mention and also notices use of unitialized variables.
- shawnz 5y agoUnfortunately Ubuntu's new bash replacement "dash" doesn't support "-o pipefail" and will error if it's present
- throw0101a 5y agodash is not a bash "replacement". dash is an implementation of the Bourne/POSIX shell. bash, when called as sh, also implements the Bourne shell as well (but badly, because it leaks bash-isms), but when bash is called as bash it is a super-set of Bourne. * https://en.wikipedia.org/wiki/Almquist_shell#dash https://en.wikipedia.org/wiki/Almquist_shell#dash * https://en.wikipedia.org/wiki/Bourne_shell https://en.wikipedia.org/wiki/Bourne_shell * https://en.wikipedia.org/wiki/Comparison_of_command_shells https://en.wikipedia.org/wiki/Comparison_of_command_shells If you want to use super-set functionality adjust your shebang accordingly.
- shawnz 5y agoIt replaced what they were using previously, which was bash. I am not saying that it is a superset of bash's functionality or that it should be expected to support bash features. EDIT: I see what you are saying now. Bash is still installed by default despite it not being aliased to /bin/sh. So it's still possible to rely on bash features if you use it explicitly. For some reason I was under the impression bash had also been aliased to dash in the default installation. Thanks for the information.
- ufo 5y agoTo be more precise, dash replaced /bin/sh. Ubuntu still includes bash in the default installation, IIRC.
- throw0101a 5y agoAs does Debian. There was a lot of gnashing of teeth when we upgraded to Debian 6 and people's (alleged) "/bin/sh" scripts broke. Most folks elected to simply change things to "/bin/bash".
- deleted 5y ago[deleted]
- deleted 5y ago[deleted]