Results 1 to 5 of 5
  1. #1
    Join Date
    May 2004
    Posts
    11

    Unanswered: changing directory from within a script

    Hi,
    i am trying to "cd" in my shell script but as soon as the script finishes execution the current directory becomes the old directory from which the script was run.

    How can i make the change permanent.

    before script execution PWD is /export/home/vbhargav

    #!/bin/sh

    TOCHANGE=/data/bmw/welcome/

    cd TOCHANGE

    echo "${PWD}" <--- this prints /data/bmw/welcome/


    exit 0

    After script execution PWD becomes /export/home/vbhargav again

    Please help

  2. #2
    Join Date
    Oct 2003
    Posts
    19
    Varun
    By default the shell runs your script in a shell which is different from the shell you logged in . So when the script exits , you come back to your own shell ,hence the old working directory .
    To make the change permanent you need to run your script in the same shell as you have logged in . To do so precede your invocation script with a "." (dot) , to specify the same shell . This will change to the directory you want .
    e.g $ . <myscript.sh> instead of $<myscript.sh>
    Let me know if that works
    Thanks
    Saurav

  3. #3
    Join Date
    May 2004
    Posts
    11

    Still not working

    I tried ./myscript.sh but the result is still the same.

  4. #4
    Join Date
    Oct 2003
    Posts
    19
    Varun
    Thanks for letting me know . Guess I forgot to say that explicitly . There should be a space between the dot and the script name e.g
    $. myscript.sh . This will tell the Shell to run the script under the same shell .

    Let me know if this works
    Thanks
    Saurav

  5. #5
    Join Date
    Aug 2004
    Location
    Rome, Italy
    Posts
    81
    you must use the $ to use the value of a variable in a shell; so:

    cd $TOCHANGE

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •