2

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

#!/bin/bash
cd /home/mtk/work/project1

File code.sh

#!/bin/bash
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
/home/mtk
$ p1.sh
$ pwd
/home/mtk    ### --> Expected /home/mtk/work/project1

Note

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

EDIT

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
else 
... other conditions

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

Please let me know, if anything is not clear.

mtk
  • 27,530
  • 35
  • 94
  • 130
  • Use a shell alias instead. – Marco Oct 08 '12 at 09:06
  • 1
    The reason that this does not work can be found here: http://unix.stackexchange.com/questions/38808/why-is-cd-not-a-program – Marco Oct 08 '12 at 09: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 09 '12 at 18:05
  • @mtk Can you not post it on the duplicate? – Michael Mrozek Oct 09 '12 at 18:38

2 Answers2

8

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; }
daniel kullmann
  • 9,527
  • 11
  • 39
  • 46
2

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.

e.g.

#!/bin/bash
#somescript.sh
echo cd /some/path

Then

eval `./somescript.sh`

Its also somewhat frowned upon for security ;o)

didster
  • 819