Change directory in a shell script

How do u change ur directory using a shell script??

same way as you do it without a shell script.

i tried giving the command 'cd /home/username' in my shell script and after executing the script i'm back in the directory where i started..

cause you work in a subshell in your script. do something like this in your script:

#!/your/shell
cd /home/username
pwd
exit 0

May be you are in the same path before and after the execution of your script. Else please explain the scenario in detail.

Cheers,
Sudhakar

I tried what u said.. But i'm still in the current directory and not in the one i specified in the shell script :frowning:

You can change the directory. But it depends on what you are trying to achieve with your script.

Run your script as

. ./myscript.sh

When you run the script in this manner, all the changes you make to env variables will remain after your exit the script.

and what is the output of "pwd" in the script?

the output of pwd is the changed directory only..

Thanks a LOT..it worked!! :slight_smile:

so everything works fine inside the script! after leaving the shell of your script you are bach in the old directory. to solve your problem follow the tips in post #7.

I did and it works.. Thanks for ur help Duke! :slight_smile:

post #11 was maybe a second to late :wink: