Skip to content

Instantly share code, notes, and snippets.

@egdoc
Last active November 13, 2019 09:44
Show Gist options
  • Select an option

  • Save egdoc/b4177e3097a27aadf931fb527e54fbd7 to your computer and use it in GitHub Desktop.

Select an option

Save egdoc/b4177e3097a27aadf931fb527e54fbd7 to your computer and use it in GitHub Desktop.
#!/bin/bash
# The command specified in a RETURN trap is executed each time
# a shell function or a script executed with the . or source builtins
# finishes executing. By default, the RETURN trap is not inherited:
sayhi() {
echo "Hi!"
}
main() {
trap "echo returned!" RETURN
sayhi
}
main "$@"
# In this case the trap will catch only when the main function
# finishes executing, not when the sayhi function does.
# The output of the script will be:
# "Hi!"
# "returned!"
#!/bin/bash
# To make the RETURN trap be inherited, we must activate
# the trace attribute:
set -o functrace
sayhi() {
echo "Hi!"
}
main() {
trap "echo returned!" RETURN
sayhi
}
main "$@"
# In this case the trap will catch both when the sayhi
# and main functions return. The output of the script
# will be:
# "Hi!"
# "returned!" <- Trap the return of the sayhi function
# "returned!" <- Trap the return of the main function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment