Bourne-Shell and fork() (was Re: K-shell variables & Do-loops)

Chet Ramey chet at cwns1.CWRU.EDU
Sat Feb 24 04:34:45 AEST 1990


In article <5649 at star.cs.vu.nl> maart at cs.vu.nl (Maarten Litmaath) writes:
>In article <642 at mwtech.UUCP>,
>	martin at mwtech.UUCP (Martin Weitzel) writes:
>)...
>){ cd somewhere; morestuff;} > whatever # sh now forks!! working directory
>)morecommands                # doesn't change for process executing morecommands
>
>Too bad... :-(

Bash does, however, handle this correctly:

cwns1$ { cd /usr/local/bin ; pwd } > foo
cwns1$ echo $PWD
/usr/local/bin				<----- CWD changes for process
cwns1$ pwd
/usr/local/bin
cwns1$ cat foo
foo: No such file or directory
cwns1$ cd -
cwns1$ cat foo
/usr/local/bin
cwns1$

ksh does this right, too.

I would think that with the ability of modern sh's (s5r2 and later) to
redirect builtin commands that this would be easy to do.  4.3 BSD /bin/sh
is probably a lost cause (maybe CSRG will replace it with bash someday).

Perhaps it's because the ability to redirect builtins was added later,
while the ability to redirect group commands has been around since V7 --
the group command execution stuff was not updated.  All in the name of
backwards compatibility. 

>)...
>)v="initial value"
>)v="new value" cmd # v is set to "new value" only in the environment of cmd
>)nextcommand $v	  # and $v expands to "initial value" here
>)
	[But not if `cmd' is a shell builtin]

>This behavior is RIDICULOUS!  A DISGUSTING BUG!

Bash gets this right, too (as does ksh):

cwns1$ v="initial value"
cwns1$ v="new value" builtin type builtin    <----- force execution of builtin
builtin is a shell builtin
cwns1$ echo $v
initial value
cwns1$

I believe this is a reasonably well-known sh bug that is slated to change.

>If the behavior of {}-grouping and loops with redirection were to change,
>it would break some scripts.  Considering the gain in elegance I wouldn't
>object though.

I believe that at least the behavior of {}-grouping *will* change, in
response to POSIX.2.  Bash is supposed to conform to that spec, so we've
tried to implement bash so that it is an example of what will be `standard
shell behavior'. 

Chet Ramey
-- 
Chet Ramey				"Can't you pay a grad student to 
Network Services Group			 read the manual for you?"
Case Western Reserve University			-- Bill Wisner,
chet at ins.CWRU.Edu				   	to Peter Honeyman



More information about the Comp.unix.questions mailing list