Skip to content

Instantly share code, notes, and snippets.

@zchee
Forked from phemmer/defer.sh
Created April 26, 2020 22:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zchee/f7597cfe32683c1d02776e98adec7482 to your computer and use it in GitHub Desktop.
Save zchee/f7597cfe32683c1d02776e98adec7482 to your computer and use it in GitHub Desktop.
bash defer function - just like go's defer()
function _run_deferred() {
local _depth="$BASHPID.${#FUNCNAME[@]}"
[[ "$_depth" != "$_deferred_depth" ]] && return
local opt=$-
set +e
for (( i=${#_deferred[@]} - 1; i >= 0; i-- )); do
eval "${_deferred[i]}"
done
[[ "$opt" == *e* ]] && set -e
}
function _defer() {
_deferred_depth="$BASHPID.${#FUNCNAME[@]}"
_deferred+=( "$(printf '%q ' "$@")" )
}
# This has to be an alias so that the `trap ... RETURN` runs appropriately.
shopt -s expand_aliases
alias defer='declare -a _deferred; declare _deferred_depth; trap _run_deferred EXIT RETURN; _defer'
function foo1() { echo foo1; }
function foo2() { defer echo foo2; foo1; }
function foo3() { foo2; }
function foo4() { echo foo4-begin; ( defer echo foo4-subshell; foo3; ); echo foo4-end; }
foo4
foo4-begin
foo1
foo2
foo4-subshell
foo4-end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment