Update: Here is my current corrected version (do not trust blindly, I had typos involved too!)
#!/usr/bin/env bash
TEMPDIR="$(mktemp -d)"
mkdir --verbose -- "${TEMPDIR}/tests"
trap 'cd -- "${TEMPDIR}/tests" && rm --verbose --one-file-system -rf "${TEMPDIR:-/invalid/615e1a5d}/tests"; cd ..; rmdir --verbose -- "${TEMPDIR}"' EXIT
And an alternative variant in case there are only files without subdirectories involved under “tests”:
trap 'cd -- "${TEMPDIR}/tests" && rm --verbose --one-file-system -f -- "${TEMPDIR:-/invalid/615e1a5d}/tests/"*; cd ..; rmdir --verbose -- tests "${TEMPDIR}"' EXIT
Note, I use --verbose to explicitly list files, because this is for my Test system. If you copy this construct to use in your own normal scripts, you might want to remove the verbose flags for normal usage.
Down below is old version:
This is just a little small question if this is secure. This script is used to create a fresh test environment that should get deleted when script ends. trap command solves that issue fine. However, I am very, very afraid of doing rm -rf in context of variables, in case the variable happens to become empty due to user error (or later changes in script). So I will do this in multiple steps.
#!/usr/bin/env bash
TEMPDIR="$(mktemp -d)"
mkdir -f -- "${TEMPDIR}/tests"
trap 'cd -- "${TEMPDIR}/tests" && rm -rf tests && cd .. && rmdir -- ${TEMPDIR}' EXIT
# Here follows the script content, creating temporary files and manipulating them...
- Use a subdirectory, so the variable is not used by itself. So we have to use
${TEMPDIR}/testseach time instead just${TEMPDIR}. - When removing all files recursively, first enter into directory with
cd, and only if that was successful delete all files recursively with a specific directory name. This should make sure thatrm -rfis only executed if the temporary directory even exist and the variable is not resolved to empty. - Off course go up one dir again and then remove the empty directory with
rmdir, which will only remove empty directories.
I personally feel confident that this construct is safe, but would like to hear your opinions. Maybe I missed something important. It would be devastating. I don’t want to try out various ways to see if one of them is working correctly.
Edit: For anyone who does not create uncontrolled temporary directories, they could just use rm -f tests/* instead, so nothing is deleted recursively. I may go that route and avoid sub-directories in my test folder.


Why do you think so? Changing directory can only be done, if it exists, otherwise cd will error out and rm command not run. After the command the temporary directory is empty and cd can go up one dir, to use rmdir on an empty directory. Which of the steps are fragile and why?
I mean, multiple commands and changing directories vs. one command. Trust me here, that’s more fragile.
And the advantage of rmdir over rm -r is, that it only deletes empty directories, is safer in some usecases. But this is moot, if you delete it’s content anyway.
And if you do have multiple commands in a trap, i recommend exporting them to a function; easier to grok.
I do not think that one command is more secure, compared to multiple steps to make sure it is secure. It can be, but it can be worse too.
Why is it safer in only some cases? I would always prefer deleting files without recursion and then deleting empty directories. Why is that moot? The point is not to use recursion.
You’re already deleting files recursively:
I don’t use rm on a variable only, but with fixed path as $TEMPDIR/tests. Therefore the actual last empty $TEMPDIR needs to be deleted separately.
But you’re still deleting files with recursion, despite saying
So if you’re using recursion to begin with,
rm -rf "$TEMPDIR"is simpler than yourrm -rf "$TEMPDIR/tests" && rmdir $TEMPDIR, and works identically except for when$TEMPDIRhas other files. But it doesn’t sound like that’s the case.If you’re always opposed to using recursion, why are you ok with using it to remove the subdirectory?
That’s the point, I do not want to do
rm -rf "$TEMPDIR", which is a variable only. Adding a fixed string like “/tests” makes sure that recursive deletion never operates on a variable only. That has the sideffect that the $TEMPDIR itself isn’t deleted, so I have to do it manually with rmdir.I’m asking why, though. The “variable only” thing doesn’t make sense. If you’re worried about it being unset or not pointing to a directory, use
test/[with-nor-d. If you can’t trust your own script to not change the variable, that’s a hell of a threat model and you shouldn’t remove anything.It’s not just about being empty, but it could point to a different directory.