/bin/sh -e function parsing strangeness

Vasil Dimov vd at datamax.bg
Sun Jun 26 09:00:27 GMT 2005


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


On Sun, Jun 26, 2005 at 11:27:02AM +0300, Vasil Dimov wrote:
> 
> Can anyone explain this:
> 
> % sh -ec 'f() { return 5 ; } ; if f ; then echo tt ; else echo ff ; fi'
> ff
> % sh -ec 'f() { return 5 ; : ; } ; if f ; then echo tt ; else echo ff ; fi'
> % echo $?
> 5
> 
> If any commands appear after the return statement in a function,
> then -e flag triggers untested command failure although function call is
> tested with `if' statement.
> 

I see, -e counts for each command inside function body, no matter that
the whole function return status is tested with `if'.

% sh -ec 'f() { ls /nonexistent ; ls / ; } ; if f ; then echo tt ; else echo ff ; fi'
ls: /nonexistent: No such file or directory
%

Probably this should be considered as a sh "feature".

-----BEGIN PGP SIGNATURE-----

iD8DBQFCvm6pFw6SP/bBpCARAu6FAKCCoGXwounURHk6o4m8huqEDtmX9wCfXqxf
M0BT7Ej0fG9Iulgr/A18MMk=
=/5jF
-----END PGP SIGNATURE-----


More information about the freebsd-hackers mailing list