Last active
November 13, 2019 09:44
-
-
Save egdoc/b4177e3097a27aadf931fb527e54fbd7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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!" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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