I don't think it's possible without the eval
. A close candidate might be:
atexit_handler() {
local EXPR
for EXPR in "${ATEXIT[@]}"; do
echo "evaluating $EXPR"
$EXPR || true
done
}
But this won't work with non-trivial expressions like this:
atexit 'for i in "a b" c; do echo $i; done'
Using the function
keyword in function declaration like this is an outdated practice:
function atexit_handler
{
local EXPR
for EXPR in "${ATEXIT[@]}"; do
echo "evaluating $EXPR"
eval "$EXPR" || true
done
}
Use the more modern style I wrote in the previous example above.
Have you considered trapping other signals too than EXIT
?
trap atexit_handler EXIT
A common practice in clean-up scripts is to handle 1 2 3 15,
but it's up to you how you want to use this.
Instead of this:
for EXPR in "$@"; do
ATEXIT+=("$EXPR")
done
A simpler way to iterate over $@
:
for EXPR; do
ATEXIT+=("$EXPR")
done
But actually, as @etan-reisner pointed out in a comment, it's silly to loop here when you can add the entire arg list in one swift move:
ATEXIT+=("$@")