Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

[BUG] 'exec' runs shell functions and builtins



In zsh, 'exec' looks up shell functions and builtins before external
commands, and if it finds one it appears to do the equivalent of running
the function or builtin followed by 'exit'. This is different from most
other shells and turns out[1] to be a bug in POSIX terms; 'exec' is
supposed to launch a program that overlays the current shell[2],
implying the program launched by 'exec' is always external to the shell.

This means that
    (exec commandname arguments ...)
is a POSIXly correct way of guaranteeing the execution of an external
command without specifying the path (something the "command" command
doesn't do in POSIX mode). This was confirmed the other day by Geoff
Clare and Chet Ramey on austin-group-l. (I seem to recall Bart asked on
this list quite some time ago if POSIX provides for a way to do this.)

This behaviour also appears to be contrary to the documentation in
zshbuiltins(1) ("Replace the current shell with an external command
rather than forking").

Test script:

testFn() {
        exec :
}
if (PATH=/dev/null exec testFn) 2>/dev/null; then
        echo "this shell execs both functions and builtins"
fi

(The only other POSIX-ish shell that zsh has this in common with is
pdksh/mksh. In pdksh this is definitely a bug as this behaviour differs
from ksh88 which pdksh is supposed to be a clone of. The POSIX spec is
also based on ksh88 behaviour.)

Confirmed down to 4.3.17 (I didn't check older versions).

- M.

[1] https://www.mail-archive.com/austin-group-l@xxxxxxxxxxxxx/msg01469.html

[2]
http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_20_14



Messages sorted by: Reverse Date, Date, Thread, Author