bash script source: No such file or directory

25,042

~ 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:
25,042

Related videos on Youtube

Khoi
Author by

Khoi

Updated on September 18, 2022

Comments

  • Khoi
    Khoi over 1 year

    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.

  • fiatux
    fiatux almost 11 years
    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
  • Sparhawk
    Sparhawk almost 11 years
    @glennjackman I'm not sure I understand. Why would priority matter for variables vs. ~?
  • fiatux
    fiatux almost 11 years
    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.
  • Sparhawk
    Sparhawk almost 11 years
    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.