I'm trying to change directory in a called bash script.
For this I tried to use cd command. But after the script ends current directory is restoring to position before the script call.
I also tried to change PATH variable but it didn't show any effect too.
Is there a way to do this?
Asked
Active
Viewed 2,279 times
1
kubudi
- 648
- 1
- 7
- 20
-
See the second answer for a way to make it work. http://stackoverflow.com/questions/255414/why-doesnt-cd-work-in-a-bash-shell-script – Paul Nov 02 '11 at 14:07
-
Sorry for dupicating question. My mistake. – kubudi Nov 02 '11 at 14:32
1 Answers
7
When you execute a shell script a new shell is started for execution of this script. This shell won't affect its parent processes.
To execute a script in your current shell's context use the source or . commands:
. cd.sh
source cd.sh
knittl
- 216,605
- 51
- 293
- 340