This prints “a”, as you’d expect:
set -e
myfun() { printf a; false; printf b; }
myfun
printf c
...because the “set -e” terminates the whole script immediately after running the “false” command.But this script prints “a-b-True”, where some people (myself included) might have expected it to print “a-False”:
set -e
myfun() { printf a-; false; printf b-; }
if myfun; then
printf True
else
printf False
fi
The “set -e” is ignored completely. Putting another “set -e” inside the definition of myfun doesn’t make any difference.I encountered the "you can't enable -e" problem years ago in a complex build system that used Bash to build embedded sysem components. The scripts were all designed on the assumption that "set -eu -o pipefail" would cause early exit on any failing commands as if doing the equivalent of "exit $?" or "return $?", and of course it is not reliable, as your example shows.
The very surprising behaviour of "set -e" inside functions was not noticed for a long time while the system was being written and used. When it was noticed, "set -e" was added inside the functions because that seemed to work for the Bash version at the time, then later it stopped working.
https://www.oilshell.org/release/latest/doc/error-handling.h...
Here's a demo of how Oil fixes it, which I mentioned in this thread:
$ osh hn.sh
a-b-True
You can add shopt --set strict_errexit at the top of your script: $ osh -o strict_errexit hn.sh
if myfun; then
^~
hn.sh:3: errexit was disabled for this construct
if myfun; then
^~~~~
hn.sh:3: fatal: Can't run a proc while errexit is disabled. Use 'try' or wrap it in a process with $0 myproc
Or just use Oil: $ oil hn.sh
<same output>
So Oil is acting like a ShellCheck at runtime, to unconditionally flag this error. It cannot be caught using syntax (which ShellCheck is limited to), because function vs. external executable is a runtime decision.After a while I stumbled upon scsh [1], which at first didn't impress me because I ran it as an interactive shell, and from this point of view it was really ugly. But then I realized that scsh was primarily meant as a way to run shell scripts, and I immediately felt in love. I had the power of a Scheme interpreter, the ability to easily use mathematical expressions (the awesomeness of Scheme's numerical tower!) and macros, and a very effective way to redirect inputs and outputs and pipe commands that was embedded in the language [2]!
In those years I used scsh a lot and developed quite complex scripts using it, it was really a godsend. Unfortunately the program got abandoned around 2006 because it was not trivial to add support for 64-bit architectures. However, while writing this post I've just discovered that somebody has revived the project and enabled 64-bit compilation [3]. I would love to see a revamp! Nowadays I use Python for complex scripts, but it's not the same as a language with native support for redirection and pipes!
While this is no doubt right, I think you could build a little abstraction that simplified piping syntax considerable. The subprocess module is not terribly convenient, although it has gotten less confusing over the years.
Python does have operator overloading, so you could even make it fancy.
That is excellent news, if it comes to anything. At present it looks like it's still dependent on the 32-bit Scheme48. Is there a roadmap for this project?
> Nowadays I use Python for complex scripts, but it's not the same as a language with native support for redirection and pipes!
Note that Guile has support for both these. But it doesn't have the shell-tool minilanguages in it that scsh does, like the one for awk.
I have also been forced to revert to bash later so it is good to know that there might be an update for it.
For simplish parallel runner https://www.gnu.org/software/parallel/ is excellent, can even automate stuff like "copy a file to remote server then run command on it" so you can make simplistic cluster computing with it
It runs your bash scripts but you can also opt into correct error handling. The simple invariant is that it doesn't lose an exit code, and non-zero is fatal by default.
See https://www.oilshell.org/release/latest/doc/error-handling.h...
Oil 0.10.0 - Can Unix Shell Error Handling Be Fixed Once and For All?
https://www.oilshell.org/blog/2022/05/release-0.10.0.html
This took quite awhile, and I became aware of more error handling gotchas than I knew about when starting the project:
e.g. it's impossible in bash to even see the error status of a process sub like diff <(sort left.txt) <(sort OOPS)
If you have bash scripts that you don't want to rewrite, try
1) run them with OSH
2) Add shopt --set oil:upgrade at the top to get the error handling fixes.
Tell me what happens :) https://github.com/oilshell/oil
I spent a long time on that, but the post didn't get read much. I think it's because it takes a lot of bash experience to even understand what the problem is.
(rehash of https://news.ycombinator.com/item?id=33075915 which came up the other day :) )
> … and it's a new language for Python and JavaScript users who avoid shell!
Why not work towards using python/JavaScript as shell languages? There was a HN thread a few days ago on Xonsh, the main python attempt at this which looks nice to me, which naturally got a lot of anti-anti-bash energy. But if we want better shell ergonomics and are willing to ditch bash, why not take popular versatile and capable languages and get them shell-appropriate? It seems like a natural use case for these languages, and even the place where they could retire to peacefully in the future.
I suppose OSH is aiming at bash compatibility, but how good or viable is that really (not a rhetorical question)? Seems to me like an all or nothing thing as someone who recently ran into some bash-zsh incompatibility.
~~~
HN thread on Xonsh: https://news.ycombinator.com/item?id=33044772
Xonsh homepage: https://xon.sh/
https://news.ycombinator.com/item?id=33122256
They're separate things because introducing MORE error handling can make your script incompatible with bash, for people who want to write code that's portable to 2 shells :-/
set -e comes from the POSIX shell language, descended from the Bourne Shell.
The examples you're using use obscure Bash features. For instance let turns an arithmetic result into a termination status, where 0 is fail (opposite to the POSIX convention and all).
set -e works to the extent that your commands have a sane termination status, which they generally do if they are standard built-ins or well-behaved utilities.
One Bash feature improves the effectiveness of set -e (or exit status testing in general). In a command pipe:
a | b | .. | z
the termination status is obtained from z. That's standard. If z indicates success, the pipeline is successful no matter how a through y terminate. Bash has a "pipefail" option to help with this.Is read(1) an obscure feature now?
The answers there are just a few of the many great examples of why bash is best avoided in almost all cases.
If you are wondering what in the world I'm talking about and I've missed your question entirely, sorry about that. My feet are tired.
This is the expectation, and the article is about why it's not that simple.
The page is probably oriented at situations where one needs to support any version of bash and any obscure/inadvisable functionality (mind that there are still devs that mix sh and bash syntax in the same script), which is very inconvenient and error prone, so manual error handling may make sense.
While there is always something insane behind the corner in Bash, with a restricted subdomain (e.g. bash 4.2+, strict shell options, and shellcheck) it's possible to progressively write reasonably solid shell scripts.
The document conclusion is somewhat biased. "to handle errors properly" implies that `-e` in inherently unreliable, which not fair - strict shell options do remove certain classes of errors, which doesn't hurt.
https://blog.habets.se/2021/06/The-uselessness-of-bash.html
I've reviewed a lot of code, bash and otherwise. I have never, not once, reviewed bash code that didn't have subtle bugs. And this is code written by smart people.
Build scripts that fail and return 0 are my nemesis.
(from the bash manual)
The ERR trap [same rules as set -e] is not executed if the
failed command is part of the command list immediately following
a while or until keyword, part of the test in an if statement,
part of a command executed in a && or || list except the command
following the final && or || [...]Makes things much saner.
This post is a better introduction than the submission: https://vaneyckt.io/posts/safer_bash_scripts_with_set_euxo_p...
It’s still much better to use it than not to use it.
But I am wondering, does zsh fare better when it comes to writing more correct scripts or is it plagued with the same issues ?
The -e terminates the script at every failure not caught by a try...catch.
With this in mind, it's easier to predict bash's behavior.
and even then, shelling out from another language when absolutely necessary can be a better option
set -euo pipefail
This is how all of my shell scripts start.set -e
let 1
let 0
set -e does exactly what you'd expect, arguably, with the exception of subshells and conditions.
And those rules are extremely simple to learn too. If you understand when a statement which might be composed of other statements would have an error, you can predict what set -e will do