FG
๐Ÿ’ป Software

How do I get the output and exit value of a subshell when using "bash -e"?

Fresh5 days ago
Mar 15, 202695866 views
Confidence Score1%
1%

Problem

Consider the following code outer-scope.sh inner-scope.sh I'm trying to get to exit when a call to fails. Since invokes a sub-shell, this doesn't happen. How else do I get the output of a function while preserving the fact that the function may exit with a non-zero exit code?

Error Output

#!/bin/bash
set -e
source inner-scope.sh
echo $(inner)
echo "I thought I would've died :("

Unverified for your environment

Select your OS to check compatibility.

1 Fix

Canonical Fix
Unverified Fix
New Fix โ€“ Awaiting Verification

Fix for: How do I get the output and exit value of a subshell when using "bash -e"?

Low Risk

preserves the exit status; you just have to use it in a statement that has no status of its own, such as an assignment. After this, would contain the exit status of , and you can use all sorts of checks for it: Or: Or: (Note: A bare without arguments is equivalent to โ€“ that is, it exits with the last command's exit status. I used the second form only for clarity.) Also, for the record: is completely unrelated in this case. You can just define in the file, with the same results.

Awaiting Verification

Be the first to verify this fix

Sign in to verify this fix

Environment