6

Here is the problem. I have a bash script which getting the path from php script. But it can't change directory to returning path;

function go_to_path
{
  path=$(php myscript)
  echo $path; # is totally okay, printing expected value 
  cd $path; # err -> no such file or directory. Directory is obviously exists
}

that stuff does not work too

eval cd $path
echo $(cd $path)
cd "$path"

I am running bash via cygwin on windows

Cyrus
  • 84,225
  • 14
  • 89
  • 153
remtsoy
  • 465
  • 1
  • 3
  • 15

2 Answers2

4

This worked fine for me:

function go_to_path
{
    path="/home/arnon/scripts"
    echo $path
    cd $path
}
ls
go_to_path
ls

It won't work if 'path' contains '~' (for instance on this example: path="~/scripts") cause that's a shell interpreted character, and not really part of the directory's name.

It also won't work if 'path' is relative to a directory that is not the directory you're running this script from. (In other words if 'php myscript' is returning a relative path make sure the relativity applies to the location your bash script is run from).

ArnonZ
  • 3,822
  • 4
  • 32
  • 42
  • 1
    php script returns absolute directory – remtsoy Aug 10 '14 at 13:30
  • This also won't work if the path contains spaces, other whitespace, or globbing characters. You should double quote the variables when used as an argument to a command, like `echo "$path"`, `cd "$path"`, etc – user000001 Aug 10 '14 at 13:33
  • if I just copypaste return value from the script change directory is working as expected. path value is completely legit – remtsoy Aug 10 '14 at 13:44
  • Does it work if you write the legit path manually in the script? – ArnonZ Aug 10 '14 at 13:55
2

ohh my. So I guess I haven't compose my question correctly. The path value is taken from dynamic algorithm. Which just echo returning value. The solution was easy, instead of echoing value I should exit it. I mean exit the script with path value.

remtsoy
  • 465
  • 1
  • 3
  • 15