Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I have a script that begin like this

#!/bin/bash
VALKYRIE=~/myProjects/valkyrie
source $VALKYRIE/cluster.conf

but when I run it it returns line 2: ~/myProjects/valkyrie/cluster.conf: No such file or directory

but the file exist and when I run source ~/myProjects/valkyrie/cluster.conf it runs fine. Any idea? I set VALKYRIE variable elsewhere so hard-code in the path isn't an option.

share|improve this question
    
I'm not 100% sure if this will help, but you could try fully quoting the variable, in case there are spaces in ~. Hence, source "${VALKYRIE}/cluster.conf". –  Sparhawk Jun 1 '13 at 3:41
    
no, it doesn't help. –  Khoi Jun 1 '13 at 3:47
1  
I think it's something to do with ~ not expanding properly. When I run your script with an intentionally fake path, the error doesn't say ~, but expands the path. Can you try replacing the ~ in your script with the absolute path? Also, try running the following in a script echo ~. –  Sparhawk Jun 1 '13 at 4:04
2  
You could also try $HOME instead of ~. –  Sparhawk Jun 1 '13 at 4:16
2  
@Khoi That explains it. ~/.pam_environment is not a shell script, so it doesn't do the common things you'd expect from a shell, such as tilde expansion and parameter expansion, so neither ~ nor $HOME will be replaced. If you move that line to ~/.profile instead, and add export in front, it should work. –  geirha Jun 1 '13 at 8:41

1 Answer 1

up vote 5 down vote accepted

~ doesn't appear to be expanding properly. When I run your script with an intentionally fake path, the error doesn't say ~, but expands the path (i.e. /home/sparhawk/fakepath not ~/fakepath. You could try using $HOME instead of ~, or using the full path in the script instead.

(I'm not sure why ~ doesn't work on your system, as your script works fine for me.)

share|improve this answer
    
When you look at the order that bash performs expansions (gnu.org/software/bash/manual/bashref.html#Shell-Expansions), you'll see that tilde expansion happens before variable expansion. That's why $HOME is better than ~ in a variable –  glenn jackman Jun 1 '13 at 4:29
    
@glennjackman I'm not sure I understand. Why would priority matter for variables vs. ~? –  Sparhawk Jun 1 '13 at 4:33
1  
it's not exactly "priority", it's simply what comes first. Consider x="~/.bashrc"; ls $x -- in the order of expansions for the "ls" command, bash looks for a tilde and doesn't find one; eventually bash sees a variable and expands it. bash does not go back and look for tildes again, at this point it's just a plain character. and there are no files in the current directory that begin with a tilde. –  glenn jackman Jun 1 '13 at 4:41
    
Ah okay. I think I get it. I've always wondered why that command fails and x=~/".bashrc"; ls $x works. Thanks for the info. –  Sparhawk Jun 1 '13 at 4:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.