Hacker News new | ask | show | jobs
by matvore 1863 days 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.

1 comments

My scripts tend to start with

set -euo pipefail

This addresses the pipeline case you mention and also notices use of unitialized variables.

Unfortunately Ubuntu's new bash replacement "dash" doesn't support "-o pipefail" and will error if it's present
dash 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/Bourne_shell

* https://en.wikipedia.org/wiki/Comparison_of_command_shells

If you want to use super-set functionality adjust your shebang accordingly.

It 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.

To be more precise, dash replaced /bin/sh. Ubuntu still includes bash in the default installation, IIRC.
As 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".