3

Possible Duplicate:
Why doesn't “cd” work in a bash shell script?

I'm attempting to execute a bash shell script './go_cd' from the UNIX command line in my working directory '/c/My_Objective'. All I expect the script to do is change to a new directory '/c/My_Objective/project'. The script output indicates that the directory has been changed to '/c/My_Objective/project, however when the script completes execution and returns to the current command line, the directory is still at '/c/My_Objective'. Why was the directory not changed?

Below is the simple Bash shell script that I'm using as a test.

#!/bin/bash
## current directory is '/c/My_Objective'
pwd
cd project
## new directory should now be '/c/My_Objective/project'
pwd

Is there a way to get the commands, ie., 'cd', executing in the new script process get passed back to the original process where I started 'go_cd' script execution?

Winston

Community
  • 1
  • 1
  • If you're used to DOS, then this is a big change of behaviour, but Unix works as described in the answer and in the x-ref'd question. – Jonathan Leffler Mar 18 '12 at 22:15
  • http://stackoverflow.com/questions/874452/change-current-directory-from-a-script : use a subshell ( end your script with exec bash ) – philippe lhardy Mar 01 '13 at 20:01

1 Answers1

6

Subprocesses don't affect the parent process. If you want your shell script to modify the existing shell, it has to be run by the existing shell, something like:

. myscript.sh
vanza
  • 9,715
  • 2
  • 31
  • 34