I have a script which looks like this:
#!/bin/bash
set -e
tmpdir=$(mktemp -d)
pushd $tmpdir
trap 'popd && rm -rf $tmpdir' EXIT
# Business logic which may succeed or fail...
/bin/false
When it exits, it popd
s just fine, but it doesn't remove the temporary directory. It seems that $tmpdir
isn't resolved in the trap, but how can we overcome that?
I verified that $tmpdir
isn't resolved by rerunning the script without -rf
in the rm
. That exited with:
rm: missing operand
instead of the expected
rm: cannot remove '/tmp/tmp.Y1SdoY5dSu/': Is a directory
Asked by Stewart
(15631 rep)
Mar 28, 2019, 10:28 AM
Last activity: Mar 28, 2019, 11:06 AM
Last activity: Mar 28, 2019, 11:06 AM