[comp.unix.shell] trap 0 in /bin/sh

ronald@robobar.co.uk (Ronald S H Khoo) (03/27/91)

Can someone explain why these two commands give different output ?
Is there some subtlety about the "trap" command that I don't understand ?

	$ ( trap 'echo foo' 0 ; true )
	$ ( trap 'echo foo' 0 ; : )
	foo

I tried this using /bin/sh on SCO Xenix, SCO Unix (orig release) and
GEC UX/63 (SVR2, I think) and SunOS 4.1.  Yes, I know they're all System V
/bin/sh, but I don't have a BSD handy to try.

-- 
Ronald Khoo <ronald@robobar.co.uk> +44 81 991 1142 (O) +44 71 229 7741 (H)

eric@mks.mks.com (Eric Gisin) (03/29/91)

In article <1991Mar27.153352.4421@robobar.co.uk> ronald@robobar.co.uk (Ronald S H Khoo) writes:

   Can someone explain why these two commands give different output ?
   Is there some subtlety about the "trap" command that I don't understand ?

	   $ ( trap 'echo foo' 0 ; true )
	   $ ( trap 'echo foo' 0 ; : )
	   foo

There is a bug in the shell.
When the last command in the list is executable (true is, : is not),
the shell tries to be clever and just does exec instead of fork/exec.
Since the shell exec()s instead of exit()s, the EXIT trap is not run.
You can work around it by putting an extra : at the end of the list:
	   $ ( trap 'echo foo' 0 ; true; : )
	   foo

   I tried this using /bin/sh on SCO Xenix, SCO Unix (orig release) and
   GEC UX/63 (SVR2, I think) and SunOS 4.1.  Yes, I know they're all System V
   /bin/sh, but I don't have a BSD handy to try.

walter@mecky.UUCP (Walter Mecky) (03/29/91)

In article <1991Mar27.153352.4421@robobar.co.uk> ronald@robobar.co.uk (Ronald S H Khoo) writes:
< Can someone explain why these two commands give different output ?
< Is there some subtlety about the "trap" command that I don't understand ?
< 
< 	$ ( trap 'echo foo' 0 ; true )
< 	$ ( trap 'echo foo' 0 ; : )
< 	foo

Looks like a bug in sh, ksh executes echo with both commands.
I suppose, the subshell for the commands in ( ... ) looks to 
the last command bevor ) and, if it's not a builtin, does only exec(2),
instead of the usual fork(2)/exec(2). So sh cannot execute the trap
commands.
-- 
Walter Mecky	[ walter@mecky.uucp	or  ...uunet!unido!mecky!walter ]

wcs) (04/01/91)

In article <893@mecky.UUCP> walter@mecky.UUCP (Walter Mecky) writes:
] In article <1991Mar27.153352.4421@robobar.co.uk> ronald@robobar.co.uk (Ronald S H Khoo) writes:
] < Can someone explain why these two commands give different output ?
] < Is there some subtlety about the "trap" command that I don't understand ?
] < 	$ ( trap 'echo foo' 0 ; true )
] < 	$ ( trap 'echo foo' 0 ; : )
] < 	foo
] Looks like a bug in sh, ksh executes echo with both commands.
Actually, true is also a ksh builtin, but ksh DOES do the right thing with
	$ ( trap ' echo foo' 0 ; ls>/dev/null )
	foo
] I suppose, the subshell for the commands in ( ... ) looks to 
] the last command bevor ) and, if it's not a builtin, does only exec(2),
] instead of the usual fork(2)/exec(2). So sh cannot execute the trap commands.
	Sounds believable, and it's interesting to see another
	difference between sh and ksh.  Other than the efficiency gain
	by skipping the fork(), is there any useful program you
	could write in /bin/sh that would break by moving to ksh,
	which depends on the presence of this bug?  Or is it pure bug?
-- 
# Bill Stewart 908-949-0705 erebus.att.com!wcs AT&T Bell Labs 4M-312 Holmdel NJ
(Little Girl:) When I grow up, I want to be a nurse      } From this week's UFT
(Little Boy:)  When I grow up, I want to be an engineer  } radio commercial
.... guess the Political Correctness Police don't run NYC's teachers' union yet