I'm trying to delete a directory referenced by an environment variable in my bash script, like so:
#!/bin/bash
export DIR='~/.docker-volumes/mydir'
rm -rf ${DIR}
Following some suggestions in https://unix.stackexchange.com/questions/61166/rm-fr-not-working and Deleting content of folder with shell script, I've also tried the following in my bash script to no avail:
rm -rf ${DIR}
find ${DIR} -type d -delete
rm -rf "$DIR"
echo $DIR | rm -rf
rm -rf -- "$DIR"
However, the directory pointed to by $DIR
isn't deleted. I can delete it directly from my Mac terminal using rm -rf ~/.docker-volumes/mydir
and it works as intended. The following also works from a bash script:
rm -rf ~/.docker-volumes/mydir
I don't like the above method though as I'd like to reference a variable, as the same path is needed in other places.
Question: Why do none of the above bash script commands which makes use of a variable, actually delete the directory? What do I need to do to delete the directory using a variable?