CD doesn't work in script

Buchbinder, Barry (NIH/NIAID) [E]
Thu May 3 17:38:00 GMT 2007

SCHLING wrote on Thursday, May 03, 2007 12:18 PM:
> I have successfully installed Cygwin on XP SP2, and would like to run
> a script which includes a change directory command (cd \etc.) 
> All the other lines of the script work except the one with cd. The
> same line works when input by hand. 
> Searching on the net I found that line endings could be critical (Lf
> instead of CrLf) and ran the script through d2u. Still no success. 
> Curiously, if the script tries a cd with a non-existing directory, I
> get a warning. Otherwise, the script runs without any problems, but
> the active directory does not change.  
> Don't know what to do next...
> Any help appreciated
> Robert

A script that starts with a #!/bin/sh or the like runs in a sub-shell,
i.e., its own process.  (I'm assuming that what you are doing.)  A "cd"
in the script changes the working directory of the sub-shell, not its
parent, the shell from which the sub-shell was launched.  When the
script exits, one finds oneself in the original directory because the
command (parent) shell has not done a "cd".

What it seems you want to do is to *source* the script.  In bash you can
do it at least two ways:
	$ . script
	$ source script

For convenience, you can set up a alias so that you don't have to
remember to type "." or "source".
	alias script='. /path/script'
Y do not need to include the path if the names of the alias and script
are different.

Unsubscribe info:
Problem reports:

More information about the Cygwin mailing list