Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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

#!/bin/bash
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
add comment

4 Answers

up vote 17 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
5  
@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
3  
is source ./script the same? –  amyassin Dec 19 '11 at 13:04
2  
@amyassin: yes, it is –  enzotib Dec 19 '11 at 13:05
show 1 more comment

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
1  
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
add comment

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
add comment

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
add comment

Your Answer

 
discard

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.