Change directory from string

Here's the problem. I have a bash script that gets the path from a PHP script. But it cannot change the directory to the returned 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 doesn't work either

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

      

I am running bash through cygwin on windows

+3


source to share


2 answers


Oh my. Therefore, I think I did not formulate my question correctly. The path value is taken from a dynamic algorithm. Which only returns the echo value. The solution was easy, instead of echoing the value, I have to get out of it. I mean the exit of the script with the path value.



+1


source


This worked fine for me:

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

      



It will not work if "path" contains "~" (for example, in this example:) path="~/scripts"

, because the shell interpreter interprets the character and is not part of the directory name.

It will also fail if the "path" is in a directory that is not the directory from which you are running this script. (In other words, if "php myscript" returns a relative path, make sure the relativity applies to the location from which your bash script is being executed).

+3


source







All Articles