How to change the working directory of invoking shell using a script?

34,026

Solution 1

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; }

Solution 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)

Share:
34,026

Related videos on Youtube

ekoeppen
Author by

ekoeppen

Updated on September 18, 2022

Comments

  • ekoeppen
    ekoeppen almost 2 years

    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.

    • Marco
      Marco over 11 years
      Use a shell alias instead.
    • Marco
      Marco over 11 years
      The reason that this does not work can be found here: unix.stackexchange.com/questions/38808/why-is-cd-not-a-progr‌​am
    • ekoeppen
      ekoeppen over 11 years
      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.
    • Michael Mrozek
      Michael Mrozek over 11 years
      @mtk Can you not post it on the duplicate?
  • augurar
    augurar over 9 years
    Shell functions are usually the way to go for anything more than a one-liner.
  • Serge
    Serge almost 9 years
    yeah those shell functions seem pretty okay to me!