Possible Duplicate:
changing current working dir with a script

I am trying to create few scripts that would change the working directory of the main shell/terminal. Not able to do so. I tried the following

File p1.sh

cd /home/mtk/work/project1

File code.sh

cd /home/mtk/templates/resusable/sampleCodes

But I am aware that the above would change it only for itself i.e. the one running currently, which would be p1.sh or code.sh in this case.

Please let me know, how would I be able to change the current working directory for the parent calling shell.

Example Run and expectation:

$ pwd
$ p1.sh
$ pwd
/home/mtk    ### --> Expected /home/mtk/work/project1


My use case is: I would like to do some processing and then place the prompt to a pre-defined directory.


I see why it would not work. And alias seems to be the solution for such case. But I would like to expand, my question:

I need to create a script that would change the working dir of the parent shell dynamically. The script will be say mycd.sh . It would have command line arguments as mycd.sh -c/-p1/-p2/ or some other user defined arguments.

Giving -c would switch the shell working dir to the respective dir. File mycd.sh would look like

.. some processing
# parameter parsing
if [ $param1 -eq "c" ]; then
    # change working dir of parent here
... other conditions

I guess, there must be some way to change the shell working directory.

Please let me know, if anything is not clear.

marked as duplicate by Mat, manatwork, daisy, Stéphane Gimenez, Renan Oct 8 '12 at 12:45

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Use a shell alias instead. – Marco Oct 8 '12 at 9:06
  • 1
    The reason that this does not work can be found here: unix.stackexchange.com/questions/38808/why-is-cd-not-a-program – Marco Oct 8 '12 at 9:13
  • Please mods, can you re-open this question. I have found 1 answer that perfectly does this which I want to post here, and would like to tell other and get the correct insight of it, to all those who said it can't be done. plzzz.. Some one please vote for re-open. – mtk Oct 9 '12 at 18:05
  • @mtk Can you not post it on the duplicate? – Michael Mrozek Oct 9 '12 at 18:38

Use aliases for this, e.g.

alias p1='cd /home/mtk/work/project1'
alias code='cd /home/mtk/templates/resusable/sampleCodes'

You should put these lines into the startup script of your shell, e.g. ~/.bashrc.

Another option would be to use shell functions. This is a bit more complicated; shell functions offer some advantages over aliases, but in this case, you do not need these extra features.

function p1 { cd /home/mtk/work/project1; }
function code { cd /home/mtk/templates/resusable/sampleCodes; }
  • 1
    Shell functions are usually the way to go for anything more than a one-liner. – augurar Feb 24 '15 at 19:57
  • yeah those shell functions seem pretty okay to me! – the0ther Aug 4 '15 at 17:15

You cant change the calling shell from your executing shell

The normal way of doing this that other apps (e.g. dircolors) seem to follow to manipulate the calling shell is to have the script echo the cd (or what ever) command (rather than execute it) then require the calling shell to wrap the call in eval

This may not work in your case as you are trying to do more than just set-up the calling shell in your script and relies on the caller actually knowing what do it.


echo cd /some/path


eval `./somescript.sh`

Its also somewhat frowned upon for security ;o)

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