JDS
JDS

Reputation: 16978

Bourne Shell trying to cd into a $variable path?

I'm trying something very simple:

MYPATH=/path/I/want/to/go/to/
...
cd $MYPATH

No good. I've tried various permutations of quotes around things and it doesn't seem to help. So how is this done?

Upvotes: 0

Views: 145

Answers (2)

just somebody
just somebody

Reputation: 19247

your shellscript runs in a separate shell, the shell from which you started is generally unaffected by the scripts actions (with the exception of things that are meant to affect it, such as filesystem changes). if you want to have a piece of code "just like" a script but affecting the invoking shell, use a function with a curly-braces body:

~/.mystuff/dostuff.func:

dostuff()
{
  cd /some/where
}

your .profile:

. ~/.mystuff/dostuff.func

start a login shell, and do

dostuff

you should be in /some/where (if it exists on your computer).

Upvotes: 1

Denys Séguret
Denys Séguret

Reputation: 382092

Is that an existing directory that you can read ?

You may want to add this before the cd if the directory may not exist :

 mkdir -p $MYPATH

Upvotes: 1

Related Questions