2

I'm trying to change the CD to a path stored in a variable. The variable gets its value from text stored in a file.

This gets the value inside the text file:

devdir=$(<\$HOME/devdir.txt)
cd $devdir

Inside the text file is the text: $HOME/bin

The error I get is:

/bin/dev: line 41: cd: $HOME/bin: No such file or directory

This is the result of trying to use the variable, compared to manually typing it:

screen shot of text so you can't copy/paste it

UPDATE: This is the snippet of Bash given with the improvement given by fabiotk, which seems to yield no result when it is within the script, however it works fine when run in the cmd window...Snippet

Here is the entire script so far: script and here is me running the given line on its own, then running the script showing it doesn't work inside: enter image description here

Jaedlo
  • 23
  • 6
  • did you mean "change the PWD to a path.." instead? – Fnr Dec 05 '17 at 11:53
  • I just meant the current directory. So i want the directory the user is currently in to be changed to that which is stored in the file – Jaedlo Dec 05 '17 at 12:01
  • 1
    *Please* don't post your information as images. See also https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-on-so-when-asking-a-question – tripleee Dec 05 '17 at 12:53
  • 1
    If you are attempting to create a script which switches the user's current working directory, you are in for trouble. This is a FAQ. https://stackoverflow.com/questions/255414/why-doesnt-cd-work-in-a-bash-shell-script – tripleee Dec 05 '17 at 13:05

2 Answers2

0

Try this:

cd $(eval "echo \"$(cat $HOME/devdir.txt)\"");

or:

eval "cd \"$(cat $HOME/devdir.txt)\"";

It will change the directory inside the script session that you are running. If you run ./script it won't cd externally

Fnr
  • 2,096
  • 7
  • 41
  • 76
  • This looks a little bit cleaner without the backslashes: `cd $(eval 'echo "$(cat $HOME/devdir.txt)"'')` – oxagast Dec 05 '17 at 12:22
  • This works perfectly in the shell, but when its in the middle of my script it just doesnt do anything :/ – Jaedlo Dec 05 '17 at 12:31
  • that's strange, are you sure you done everything right? I made a simple script here and it worked: `cd $(eval "echo \"$(cat $HOME/devdir.txt)\""); ls -la;` and the `ls` printed the contents of devdir's path – Fnr Dec 05 '17 at 12:45
  • I'll add the whole script if that will show you anything useful? It should be running that section due to the echo being shown before it, but it just doesn't change the directory. – Jaedlo Dec 05 '17 at 12:47
  • What's with the `echo`? Why not just `eval "cd '$(cat $HOME/devdir.txt)'"`? – tripleee Dec 05 '17 at 12:52
  • @tripleee without the echo I ended up with the same problem as OP's – Fnr Dec 05 '17 at 12:54
  • You need to `eval` some command but it might as well be the `cd` directly, which incidentally also simplifies the solution by about two orders of magnitude. – tripleee Dec 05 '17 at 12:57
  • 1
    ... Though the single quotes in my comment are inhibiting that so change those to escaped double quotes. – tripleee Dec 05 '17 at 13:00
0

The value of $HOME is not getting expanded. If you want to expand it, the eval posted in another answer might help; but we generally want to avoid eval if we can, especially if you only need this specific variable to be expanded.

devdir=$(sed "s%^\$HOME/%$HOME/%" $HOME/devdir.txt)
tripleee
  • 175,061
  • 34
  • 275
  • 318
  • So should this work if i just 'cd devdir' after implementing that section instead? – Jaedlo Dec 05 '17 at 12:54
  • No, the syntax to expand the variable is `cd "$devdir"` (the quotes are not strictly required in this limited scenario, but we see way too many examples where people post code which is broken because they forgot the quotes). – tripleee Dec 05 '17 at 12:56
  • I'm still getting that same error as the original post now with your code – Jaedlo Dec 05 '17 at 13:00