1

I'm having trouble trying to write a simple bash function that will move a file to a designated folder. The code I have written is as follows:

mv_file() {
  DESTINATION_FOLDER="~/Whatever"
  mv $1 $DESTINATION_FOLDER
}

The issue is however that $1 often contains escaped characters such as ~/\[Bracketed\]File which when passed to $1 no longer contains the \ character and ends up looking for ~/[Bracketed]File which isn't a valid file/path. I've gone through the other questions on this topic yet I don't think the solutions pertain to this problem. Any suggestions?

aMat
  • 305
  • 1
  • 2
  • 10
  • Wouldn't quoting the variable help this? `mv "$1" $DESTINATION_FOLDER` – marekful Oct 19 '14 at 20:41
  • Nope, unfortunately when I quote it, the forward slashes still don't exist. For example when I `echo "$1"` I get `~/[Bracketed]File` :( – aMat Oct 19 '14 at 20:50
  • Then I guess you need to _pass_ to command line params in quotes. E.g. `yourscript.sh "~/\[Bracketed\]File"`. – marekful Oct 19 '14 at 20:51
  • Hmm I see, but that would be at the expense of auto-fill which would be cumbersome. I could auto-fill, and then wrap in quotes, but isn't there a better way? – aMat Oct 19 '14 at 20:55
  • I don't really get your point about about auto-fill. Is this script automated somehow? Is the source folder parameter a user provided value? – marekful Oct 19 '14 at 20:58
  • Oh sorry, I just meant that when you write quotes, tab-completion won't work to pick up the file name. – aMat Oct 19 '14 at 21:01
  • You need to escape all of the special characters, which can be done with `printf`. Potential duplicate of http://stackoverflow.com/questions/5608112/escape-filenames-using-the-same-way-bash-do-it – whereswalden Oct 19 '14 at 21:23

1 Answers1

0

It seems that two changes are needed. First, you likely want to replace:

DESTINATION_FOLDER="~/Whatever"

with:

DESTINATION_FOLDER=~/"Whatever"

The shell only does tilde expansion is the tilde is unquoted.

Second, if the first argument to the script is correctly quoted (not clear from the question), then you need to replace:

mv $1 $DESTINATION_FOLDER

with:

mv "$1" "$DESTINATION_FOLDER"
John1024
  • 109,961
  • 14
  • 137
  • 171
  • Thanks for the tip with tilde, didn't know :). Unfortunately when I wrap `$1` in quotes however it doesn't change the forward slashes from disappearing. For example when I `echo "$1"` I still get `~/[Bracketed]File` – aMat Oct 19 '14 at 21:04
  • @aMat Would you clarify the issue with the bracketed path. You write "`~/[Bracketed]File` which isn't a valid file/path". On your filesystem, what is the valid path? Also, could this be another tilde inside quote issue? – John1024 Oct 19 '14 at 21:07
  • Yea sure. So I would evoke the function by writing `mv_file ~/\[Bracketed\]File` and then it would spit out the following error `mv: rename /Users/aMat/[Bracketed]File to ~/Whatever: No such file or directory`. I suspect the problem is that I need the escape characters there for the `mv` call. – aMat Oct 19 '14 at 21:15
  • Update! Sorry for the confusion man but you were absolutely right on both accounts! My problem was i didn't start a new bash session so i didn't register any of the changes. Thanks again man, both tips were needed to fix this! – aMat Oct 19 '14 at 21:26