Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

`-o pipefail` is not without its gotchas

    bash -eo pipefail -c "cat /etc/services | head -1; echo never got here"


I hate pipefail.

I find set -e also has some gotchas. '|| die' added to the end of each line is uglier but more obvious in many cases.


What is the gotcha here? You’re telling it to pipefail and exit on fail, so it exits.


The gotcha is perhaps failing to recognize that cat will actually fail in this simple pipeline because its output is truncated "unexpectedly" by head exiting after completing its job properly. (Maybe you could argue that head should be a good citizen and read its input to completion)

Where this will work as expected:

    bash -eo pipefail -c "head < /etc/services  -1|cat ; echo got here"


But head is a being a good citizen by doing only what it needs to do and not anything else. Otherwise things like `grep X <5GBfile | head -n1` would process the whole file and not just until the first match.




Consider applying for YC's Winter 2026 batch! Applications are open till Nov 10

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: