TODO #!/usr/bin/env bash # Provides an "atexit" mechanism for scripts. # Array of commands to eval. declare -a __atexit_cmds = '' # Helper for eval'ing commands. proc __atexit { for cmd in [$(__atexit_cmds[@])] { eval $(cmd) } } # Usage: atexit command arg1 arg2 arg3 proc atexit { # Determine the current number of commands. var length = $(#__atexit_cmds[*]) # Add this command to the end. setglobal __atexit_cmds[${length}]="${*}" # Set the trap handler if this was the first command added. if [[ ${length} -eq 0 ]] { trap __atexit EXIT } }