tags:

views:

42

answers:

3

In my .sh file, I have this, cp $file $SOME_PATH, while $SOME_PATH is exported as export SOME_PATH="~/path/to/path/". But when I ran this .sh file, I got error message saying like * no such "~/path/to/path/" exists.* I replaced ~ as $HOME, then the error was gone. So what's up here with the tilde?

Thanks in advance.

A: 

Remove the quotation marks on your export:

export SOME_PATH=~/path/to/path/
chrisaycock
Thanks, but this seems not available, either.
dutor
Oh, sorry. Actually, I used cp $file "$SOME_PATH". ^_^
dutor
Does it work with all quotation marks removed? You basically have to prevent literal interpretation.
chrisaycock
A: 

You want the shell to expand ~, but it will not be expanded in a script since it's treated as a literal string "~". You can force expansion via eval like this.

#!/bin/bash

homedir=~
eval homedir=$homedir
echo $homedir # prints home path
birryree
Thanks. So the $HOME is preferred.
dutor
@Downvoter(s): Any reasons?
birryree
+2  A: 

use

SOME_PATH=~/path/to/path/

if you path have spaces, quote it

SOME_PATH=~/"path with spaces"
ghostdog74
@ghostdog: But what if the path contains space ?
codaddict
"path with space" -> path\ with\ space
Eric Towers
+1, that works.
codaddict