Sample Header Ad - 728x90

How can I get bash to exit on backtick failure in a similar way to pipefail?

82 votes
7 answers
37805 views
So I like to harden my bash scripts wherever I can (and when not able to delegate to a language like Python/Ruby) to ensure errors do not go uncaught. In that vein I have a strict.sh, which contains things like: set -e set -u set -o pipefail And source it in other scripts. However, while pipefail would pick up: false | echo it kept going | true It will not pick up: echo The output is 'false; echo something else' The output would be The output is '' False returns non-zero status, and no-stdout. In a pipe it would have failed, but here the error isn't caught. When this is actually a calculation stored in a variable for later, and the value is set to blank, this may then cause later problems. So - is there a way to get bash to treat a non-zero returncode inside a backtick as reason enough to exit?
Asked by Danny Staple (2181 rep)
Oct 21, 2011, 10:42 AM
Last activity: Apr 29, 2025, 03:43 PM