Search code examples
linuxbashrm

rm adding extra backslash to variable


I'm writing a very simple script, that will interact with my git repository, but I've reached a point, where I can't figure out why the following is happening.

Having:

destPath='~/Dropbox/Shared/Alex\&Stuff'
destFile='file.zip'

#git archive --format zip --output $destFile master

echo $destPath/$destFile

rm $destPath/$destFile

The echo outputs the correct path:

~/Dropbox/Shared/Alex\&Stuff/file.zip

But the rm fails with the following:

rm: cannot remove ‘~/Dropbox/Shared/Alex\\&Stuff/file.zip’: No such file or directory

So, why the extra backslash is added when rm is executed? Alex\\$Stuff instead of Alex\$Stuff ?


Solution

  • Tilde character needs to be outside quote to be expnded:

    destPath=~/Dropbox/Shared/Alex\&Stuff
    
    destFile='file.zip'
    
    #git archive --format zip --output $destFile master
    
    echo "$destPath/$destFile"
    
    rm "$destPath/$destFile"