Open

About User

Questions Asked: 5

Answers Given: 5

0
  • Open

How can I make one bash subshell exit the main calling shell script?

0 Likes 0 Favourites 0 Followers 0 Comments
Answers(1)
#!/bin/bash

set -e

function foo() {
  # commands that might fails and I want to exit my script
  ...
  echo "result I need as output"
}

my_var=$(foo)

echo "I don't want this if there is an error inside foo"

Using set -e (in bash 4.4.19) does not seem to work with subshells i.e. the last echocommand is still being executed). How can I write the code to make the script exit if any of the commands inside foo terminate with non-zero exit code.

 

0