0

I have this command:

echo $(bash main.sh)

In main.sh, I run a java program that simply throws an exception immediately. The exit code of echo is 0 which means the command succeeded. Is there anyway to make it fail with the exit code from bash main.sh?

The use of echo is not mandatory, it can be replaced with another program.

1 Answer 1

1

You can save the command substitution in a variable first, which will let you keep the exit code:

output=$(bash main.sh)
exit_code=$?

some-command "$output"
(exit "$exit_code") # now $? will have exit status of the script

Example:

$ foo=$(echo bar; false)
$ ret=$?
$ echo $ret
1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .