[comp.unix.wizards] {} grouping in sh and ksh

davidsen@steinmetz.ge.com (William E. Davidsen Jr) (10/05/88)

  I recently posted an answer to a question and have been told that me
reply is true for ksh, but not for sh. Let me clarify the action of both
shells. I tested the sh operation on both V.2 and V7, so I am reasonably
certain of the action.

  In sh, actions grouped by {} delimiters will be treated as a unit and
may be used with &&, ||, and redirection. If redirection is used the
commands will be executed in a subshell. After the commands have been
run, if a subshell is not used the working directory is reset, such that
it always works as if a subshell were used.
  Ex:	{ cd ..; ps -l; pwd; ps -l; } && pwd
	{ cd ..; ps -l; pwd; ps -l; } > x && pwd

In the example with redirection the first ps will show a subshell
running.

  Even if a subshell is used, the last command is execed without a
fork, so the second ps will *never* show a subshell. This muddied the
water when I was investigating the action.

  In ksh a subshell is never used. After execution of the commands the
working directory is not reset. This is exactly as documented. The
examples above will produce the same output.

  I apologize if this mislead anyone, I know how ksh works, and a simple
test with sh before my last posting didn't show the details.
-- 
	bill davidsen		(wedu@ge-crd.arpa)
  {uunet | philabs}!steinmetz!crdos1!davidsen
"Stupidity, like virtue, is its own reward" -me

m5@lynx.UUCP (Mike McNally) (10/06/88)

In article <12306@steinmetz.ge.com> davidsen@crdos1.UUCP (bill davidsen) writes:
>  In sh, actions grouped by {} delimiters will be treated as a unit and
>may be used with &&, ||, and redirection. If redirection is used the
>commands will be executed in a subshell. After the commands have been
>run, if a subshell is not used the working directory is reset, such that
>it always works as if a subshell were used.
>  Ex:	{ cd ..; ps -l; pwd; ps -l; } && pwd

Gee, when I try the first example on my BSD machine, sure enough the
working directory is changed for the whole shell.  In other words, the
shell doesn't do any fancy switching back to the old wd.  Maybe
System V is different.








-- 
Mike McNally                                    Lynx Real-Time Systems
uucp: {voder,athsys}!lynx!m5                    phone: 408 370 2233

            Where equal mind and contest equal, go.