Skip to content

Instantly share code, notes, and snippets.

@multun
Last active April 8, 2020 18:24
Show Gist options
  • Save multun/962a301694de3b4738e27242becee096 to your computer and use it in GitHub Desktop.
Save multun/962a301694de3b4738e27242becee096 to your computer and use it in GitHub Desktop.
bash space safe cleanup hooks
__cleanup_hook_i=0
__cleanup_hook () {
for ((i=__cleanup_hook_i - 1; i >= 0; i--)); do
eval "\"\${__cleanup_hook_action_${i}[@]}\""
done
}
trap __cleanup_hook EXIT
register_cleanup () {
eval "__cleanup_hook_action_$((__cleanup_hook_i++))=( \"\$@\" )"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment