Skip to content

Instantly share code, notes, and snippets.

@cobbr2
Last active April 2, 2020 18:53
Show Gist options
  • Save cobbr2/d605ab39fcf93edde4e79b5d9be056d6 to your computer and use it in GitHub Desktop.
Save cobbr2/d605ab39fcf93edde4e79b5d9be056d6 to your computer and use it in GitHub Desktop.
Bash `-e` does not propagate to called scripts, but does to functions.
#!/bin/bash
source ./bash_fn
set -e
./bash_script
bash_fn
#!/bin/bash
bash_fn() {
echo "function: Before failing command"
false
echo "function: After failing command"
}
echo "script: Before failing command"
false
echo "script: After failing command"
$ ./bash_caller
script: Before failing command
script: After failing command
function: Before failing command
@cobbr2
Copy link
Author

cobbr2 commented Apr 2, 2020

When you run a shell script, it's in a child process. Bash functions run in the same process as their caller.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment