Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

change directory from string

Tags:

bash

path

cd

cygwin

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

like image 679
remtsoy Avatar asked Aug 10 '14 09:08

remtsoy


People also ask

How do I change the script directory?

To change directories, use the command cd followed by the name of the directory (e.g. cd downloads ). Then, you can print your current working directory again to check the new path.


1 Answers

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

like image 148
Arnon Zilca Avatar answered Sep 20 '22 17:09

Arnon Zilca