Sample Header Ad - 728x90

How to safely remove directory tree in script with path defined by a variable

1 vote
0 answers
649 views
This is a very basic question, and surely has been asked before, but I am having trouble finding the answer or guidance. There are e.g. these but they don't quite hit the mark: - https://unix.stackexchange.com/questions/653796/how-to-safely-remove-all-files-including-hidden-files-from-current-directory - https://unix.stackexchange.com/questions/442346/safely-define-an-environment-variable Essentially I just want to remove a temporary directory tree as part of a bash script, like so rm -rf $my_path But I'm a bit paranoid that somewhere else in the script it could be possible for $my_path to expand out into / or something equally bad. For example if somewhere there is my_path=$var1/$var2 and both $var1 and $var2 end up defaulting to empty strings or some such. What is the best practice way to write this kind of command into a script to avoid accidental enormous problems?
Asked by Ben Farmer (131 rep)
Jul 15, 2021, 01:22 AM