Skip to content

Instantly share code, notes, and snippets.

@Oppodelldog
Created November 27, 2019 20:29
Show Gist options
  • Save Oppodelldog/133e349df97bd645f2ad4a80ed219e9f to your computer and use it in GitHub Desktop.
Save Oppodelldog/133e349df97bd645f2ad4a80ed219e9f to your computer and use it in GitHub Desktop.
shell - nested traps
#!/bin/bash
err_report() {
echo "error in inner script: $(caller):$1"
}
trap 'err_report $LINENO' ERR
echo "INNER SCRIPT"
false
#!/bin/bash
err_report() {
echo "error in outer script: $(caller):$1"
}
trap 'err_report $LINENO' ERR
echo "OUTER SCRIPT"
./inner.sh
@Oppodelldog
Copy link
Author

I wanted to know what happens if I have nested shell scripts both adding a error trap.

the output of calling ./outer.sh :

OUTER SCRIPT
INNER SCRIPT
error in inner script: 14 ./inner.sh:14
error in outer script: 11 ./outer.sh:11

This is what I wished it would be, so I can add some cleanup in the nested script, but the outer scripts also will do its cleanup.

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