12

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.

12
  • 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, 2013 at 3:41
  • no, it doesn't help.
    – Khoi
    Jun 1, 2013 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, 2013 at 4:04
  • 2
    You could also try $HOME instead of ~.
    – Sparhawk
    Jun 1, 2013 at 4:16
  • 4
    @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, 2013 at 8:41

1 Answer 1

10

~ 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.)

5
  • 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 Jun 1, 2013 at 4:29
  • @glennjackman I'm not sure I understand. Why would priority matter for variables vs. ~?
    – Sparhawk
    Jun 1, 2013 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. Jun 1, 2013 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, 2013 at 4:43
  • My path was within the quotes so I had to remove the quotes and it worked.
    – nick-s
    Oct 5, 2022 at 2:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .