1

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?

AjLearning
  • 419
  • 4
  • 13
  • @GordonDavisson Could you please show the question this is potentially a duplicate of? The 'marked as duplicate' message does not mention it. – AjLearning May 13 '19 at 02:51
  • The UI is slightly confusing: look at the other yellow message right below the title – that other guy May 13 '19 at 02:52
  • 1
    There should be two duplicates listed at the top of the question. In any case, the short answer is that `~` doesn't expand to your home directory when it's in quotes. – Gordon Davisson May 13 '19 at 02:53
  • Thanks guys, got it. I was sure SO duplicates were mentioned as part of the section where it's marked a duplicate. Perhaps they've changed the UI. – AjLearning May 13 '19 at 02:54
  • I may be thinking of 'Possible duplicate of' instead of 'marked as duplicate' :) – AjLearning May 13 '19 at 02:55

0 Answers0