Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to run a script to simply change the current working directory:

cd web/www/project

But, after I run it, the current pwd remains unchanged! How can I do that?

share|improve this question
Can your provide more information? Some thing like your directory structure, or the context... – favadi Dec 19 '11 at 8:06
up vote 24 down vote accepted

It is an expected behavior, and already discussed several times.

The script is run in a subshell, and cannot change the parent shell working directory. Its effects are lost when it finishes.

To change directory permanently you should source the script, as in

. ./script
share|improve this answer
Thank you! That worked! :) – Sony Santos Dec 19 '11 at 8:15
@Sony: Note that you should use return to escape from a script sourced in this way, not exit - they are like shell functions, and exit will exit the shell that sourced the script. – Charles Stewart Dec 19 '11 at 8:19
@CharlesStewart In fact, I'm not familiar with sourced scripts. Thank you! – Sony Santos Dec 19 '11 at 12:56
is source ./script the same? – amyassin Dec 19 '11 at 13:04
@amyassin: yes, it is – enzotib Dec 19 '11 at 13:05

For small tasks such as this, instead of creating script, create an alias like this,

$ alias cdproj='cd /dir/web/www/proj'

You should add this to your .bashrc file, if you want it set for every interactive shell.

Now you can run this as $ cdproj.

share|improve this answer
That's a good tip! I'll consider to use it for some tasks. Thank you! :) – Sony Santos Dec 19 '11 at 20:26
You can also have the script echo the commands to be executed, and then use eval `./script` or eval $(./script) to execute those commands. This is a common approach for commands that need to update the invoking shell's environment. – Keith Thompson Dec 20 '11 at 10:41
Just be very careful about what you output if you are going to go the eval approach. – jw013 Sep 14 '12 at 20:08

While there are answers that do the exact action that you want, a more standard method for such purpose is to create symbolic link:

ln -s ~/web/www/project proj   #use full path to dir!

Then you could cd to the directory using the name proj:

cd proj

This method is more flexible because you could access files using the short name without cd:

ls proj/   #note the endslash!
vim proj/file.x
share|improve this answer

If you change between directories far away in the filesystem. I will recommend autojump.

share|improve this answer
awesome project (autojump). Thanks – MountainX Jul 18 '13 at 21:02

Use exec bash at the end

A bash script operates on its current environment or on that of its children, but never on its parent environment.

However, this question often gets asked because one wants to be left at the bash prompt in a certain directory after the execution of a bash script from another directory.

If this is the case, simply execute a child bash instance at the end of the script:

cd web/www/project
exec bash
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.