Subject: Re: Strange behavior of 'cd' command
To: Marechal, Laurent <lmarechal@rational.com>
From: Julio Merino <jmmv@menta.net>
List: netbsd-help
Date: 01/31/2003 18:06:45
I think that the shell executes a subshell when it has to run a pipe
(so the cwd is restored when the subshell exits).



On Fri, 31 Jan 2003 17:45:27 +0100
"Marechal, Laurent" <lmarechal@rational.com> wrote:

> Hi,
> 
> NetBSD kaori 1.6 NetBSD 1.6 (KAORI_SM) #0: Tue Dec 17 12:23:22 UTC 2002
> sh shell from the 1.6 release.
> 
> 
> By mistake I entered a wrong command but even like that the result was not
> the one expected.
> 
> The command was :
> 	cd test | grep blah
> 
> test is a directory. I know this command will produce no output. But for me
> here is what should happend:
> 1. the 'cd test' command is executed.
> 	No stdout is generated. 
> 	The directory is changed.
> 2. the stdout from (1) is piped into the grep via the stdin
> 3. the 'grep blah' is printing to stdout the match between his stdin (cd
> test stdout) and 'blah'
> 4. there should be no ouput.
> 
> All is running like that but, the directory is not changed.
> 
> # ls -l
> [...]
> drwxr-xr-x   2 root  wheel    512 Nov 13 16:33 scratch
> drwxr-xr-x   2 root  wheel    512 Jan 10 10:14 scripts
> # pwd
> /root
> # cd scratch | grep blah
> # pwd
> /root
> #
> 
> I know that this command 'cd test | grep blah' is useless and should not
> report anything, as I said it was a mistake. But why is the cd command is
> not executed ?
> 
> As a test I ran it also on other OS with the same result (the shell was
> always different):
> - NetBSD 1.6 under sh
> - Linux RH 7.3 under bash
> - FreeBSD 4.? under ash
> - SunOS under csh
> 
> 
> can it be the the shell is really smart and just doesn't run the command as
> he find-out there will be no output ?
> 
> Laurent\


-- 
Julio M. Merino Vidal <jmmv@menta.net>
The NetBSD Project - http://www.NetBSD.org/