back
110 comments
A lot of commenters suggesting "pipefail" aren't realising the full extent of the problem. Here's an example that might be clearer, from https://david.rothlis.net/shell-set-e

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.
It's always been odd that Bash has hundreds of options for controlling its behaviour in non-standard ways, via "set", "shopt" and environment variables, but it's never had an option to make "-e" do the obviously natural behaviour,,

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.

I call this the "disabled errexit quirk" which leads to the "if myfunc" pitfall:

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.
This is a separate problem from the pipe one. Anyway, if you're looking that behavior, you need to use `&&` instead of the semicolon.
In the 2000s, I was running extensive sets of simulations and data reduction scripts for a scientific experiment, and I was heavy relying on scripts to run the programs, collect the results, and distribute them over several servers. At first I developed those scripts using bash, but I needed to do math and complex iterations over file names and different parameter files, and I continuously stumbled upon weird behaviors and had to rely to hard-to-understand quirks like the ones explained in the article (which bit me more than once!).

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!

[1] https://scsh.net/

[2] https://scsh.net/docu/html/man-Z-H-3.html#node_chap_2

[3] https://github.com/scheme/scsh

Whenever I need a quick script I use bash. If that script goes beyond 20 lines or has anything but the simplest `if` or `&&`, I've learned time and time again that I should rewrite it (python for example)
> 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.

> However, while writing this post I've just discovered that somebody has revived the project and enabled 64-bit compilation [3].

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 was very surprised to see scsh mentioned in this thread. I'm glad to read that it was of some use for you.
I had a very similar experience with using scsh for shell scripts instead of bash and it was great and far more convenient than using Python or any other language with poor support for the shell style of command execution.

I have also been forced to revert to bash later so it is good to know that there might be an update for it.

Have you seen python's sh module?
Wait till you learn about Python lmao

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

All of these problems are fixed in OSH.

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 :) )

Never used OilShell (OSH) before, but this opening line on their home page struck me:

> … 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/

Addendum: You need shopt --set oil:upgrade strict:all to catch this, or just use Oil. Demo here:

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 :-/

Mainly, the reason why Bash's "set -e" doesn't do what you expected is this.

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.
TFA talks about pipefail and how it sometimes fails when command writes too much.

Is read(1) an obscure feature now?

I can highly recommend using Shellcheck [0] when writing Bash, it also has extensions for VS Code and other IDE's. It makes writing Bash much easier.

[0] https://github.com/koalaman/shellcheck

I moved over to shellharden a while ago. It can actually apply the suggestions it makes. Aside from that, my employer is somewhat disapproving for GPLv3 tools, but MPL that shellharden uses is essentially auto-approved.

https://github.com/anordal/shellharden

Agree, this is the way.
Short answer: because it's Bash. No syntax or language construct will do what you expected.
I tend to think of Bash as a syntax-free language. There are no clear rules, you just have to kind of make it work every time. It's like the programming version of a freestyle rap or Parkour.
Sometimes it also depends on the specific version, like when you need to deal with empty arrays - https://stackoverflow.com/questions/7577052/bash-empty-array...

The answers there are just a few of the many great examples of why bash is best avoided in almost all cases.

You don't have to be hyper-aware of false positives with "set -euo pipefail". False positives bring themselves to your attention during testing, while false negatives don't announce themselves at all. It's almost always preferable for your code to incorrectly fail and force you to stick "|| true" on the end, than to incorrectly succeed and let you miss the bug.
There are false negatives, such as the one mentioned in the post after “A particularly dangerous pitfall with set -e is combining functions with conditionals.”
All set -e does is halt further execution of your script if any line exits above 0. There is really nothing bash can do about this. It can't perform a psychological evaluation on why a program is not giving the expected output. And most of the time, when something fails, if it were made by a less than serious programmer (like myself) they don't bother to exit on error correctly with a code above zero. But if you are building a script collection, or python utility or even an binary, simply exit 1 if you encounter a general error. Then further up the chain in your shell scripts the error can be handled properly. There are more specific error codes you can use that might be helpful to your shell script.

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.

> All set -e does is halt further execution of your script if any line exits above 0.

This is the expectation, and the article is about why it's not that simple.

Shell scripts can be thought like C++. They can be sanely managed only if one adopts a strict subset of functionalities.

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.

Also noteworthy, this post of mine with authoritative text from GNU docs. Read "(Un)Portable Shell Programming"

https://news.ycombinator.com/item?id=31678176

Annoyingly, when a process is terminated by an unhandled signal (say, SIGTERM), it is treated as if it exited with a nonzero exit code. This can make it tricky to use non-builtin commands as conditions in "if" statements, since there's always the potential edge case where the "if" block is skipped because of a signal that the condition received.
Because bash error handling is a thousand blades and no handle.

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.

If you modify your PS1 to include $? It makes writing shell scripts that rely on exit codes a lot easier. It should be the default in my opinion.

Build scripts that fail and return 0 are my nemesis.

This is really interesting. The case pointed out in Ex.3 is pretty hilarious:

(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 || [...]
I use "set -Eeuo pipefail" in pretty much all bash scripts. (and often -x as well)

Makes things much saner.

This post is a better introduction than the submission: https://vaneyckt.io/posts/safer_bash_scripts_with_set_euxo_p...

One of the reasons I created Next Generation Shell. It has exceptions. So "if $(grep ...)" works correctly as opposed to bash. grep exit codes: 0 - found, 1 - not found, 2 - error. bash can not handle this correctly in if. There are just two branches for 3 exit codes. NGS has two branches and exception that can be thrown. Yep, every single "if grep ..." in bash is a bomb.
The article is great, but I wholly disagree with the conclusion “don’t use -e”.

It’s still much better to use it than not to use it.

I am using zsh but only because it comes with a little more out the box and because zle's excellent vi mode (supports text objects for the win)

But I am wondering, does zsh fare better when it comes to writing more correct scripts or is it plagued with the same issues ?

Few people understand that if...then in bash is actually a form of try...catch.

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.

mountain out of a molehill imo. there’s a certain point where you’ll be fighting shell more than it’s helping—choosing another language is the better choice. i’m not a wizard but i feel like i’ve developed a decent intuition for what’s sane to do in shell and what needs something else.

and even then, shelling out from another language when absolutely necessary can be a better option

What's the best and smallest choice to replace admin/ops bash scripts ? Python ? Python with some lib ? Ruby ? Lua ? Rust ?
If anyone reading these threads is over 70yo, please reply to this comment with the correct explanation.

    set -euo pipefail
This is how all of my shell scripts start.
the `read -r foo < configfile` might be obscure, but every line in a POSIX text file ought to have a newline terminator. If this is worth erroring out on probably depends on context.
In your bash shell, type:

set -e

let 1

let 0

Because you don't understand what it is meant to do.
I'm not sure what this person did expect. That bash magically parsed the output and memory of running programs, and read the user's thoughts, to determine if the state of the program indicates a condition that the user would consider an error?

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