Reputation: 2725
Trying to write a simple script to copy some files in OS X 10.9. Here's the content..
SRC_DIR="~/Library/Preferences-Old"
DST_DIR="~/Library/Preferences"
FILEN="test.txt"
cp $SRC_DIR/$FILEN $DST_DIR
Gives me the output:
cp: ~/Library/Preferences-Old/test.txt: No such file or directory
Of course, the above is wrong. The exact same cp command in terminal directly does the trick. What am I doing wrong here?
Upvotes: 4
Views: 27934
Reputation: 532428
~
is one of the few exceptions to the rule "When in doubt, quote". As others have pointed out, a quoted ~
is not subject to expansion. However, you can still quote the rest of the string:
SRC_DIR=~"/Library/Preferences-Old"
DST_DIR=~"/Library/Preferences"
Note that depending on the values assigned to the two *_DIR
variables, it's not enough to quote the values being assigned; you still need to quote their expansions.
FILEN="test.txt"
cp "$SRC_DIR/$FILEN" "$DST_DIR"
Upvotes: 8
Reputation: 4631
As already mentioned double-quotes disabled ~
expansion.
Better approach is to use HOME
variable:
SRC_DIR="$HOME/Library/Preferences-Old"
DST_DIR="$HOME/Library/Preferences"
Upvotes: 2
Reputation: 3727
Your double-quotes are preventing the shell from converting your ~
into an actual path. Observe:
$ echo ~
/home/politank_z
$ echo "~"
~
~
isn't an actual location, it is shorthand for the path of your home directory.
Upvotes: 6