I am trying to change a directory to home/developer.
I used cd home/developer in my shell script.
After executing script it is again coming to original directory,where I executed shell script.
Asked
Active
Viewed 1.7k times
6
Radu Rădeanu
- 166,822
- 48
- 327
- 400
Sujatha
- 73
- 1
- 1
- 6
-
possible duplicate of [Why doesn't "cd" work in a shell script?](http://askubuntu.com/questions/481715/why-doesnt-cd-work-in-a-shell-script) – muru Dec 04 '14 at 18:16
1 Answers
11
When you start your script a new process is created that only inherits your environment. Your current environment stays as it is. You can start your script like this if you want to change the current directory from a script:
. script.sh
or
source script.sh
The . (source is the long version of .) will evaluate the script in the current environment so it might be altered.
When a script is run using
sourceit runs within the existing shell, any variables created or modified by the script will remain available after the script completes. In contrast if the script is run just asscript.sh, then a separate subshell (with a completely separate set of variables) would be spawned to run the script.
Sources:
Radu Rădeanu
- 166,822
- 48
- 327
- 400
-
-
2@Sujatha Can you post your script on http://paste.ubuntu.com/ ? I suspect that you have `exit` command inside the script. If so, it is normal. – Radu Rădeanu Jul 29 '13 at 07:32
-