2020-01-31 21:26:33 +0000, Stephane Chazelas:
[...]
> You might have been mislead by Solaris' /usr/xpg4/bin/sh
> 
> On Solaris 11,
> 
> $ /usr/xpg4/bin/sh -c 'type printf'
> printf is a shell builtin
> 
> But
> 
> $ truss -ft execve /usr/xpg4/bin/sh -c 'printf "x"'
> 1382:   execve("/usr/xpg4/bin/sh", 0x7FEF43B27CC8, 0x7FEF43B27CE8)  argc = 3
> 1383:   execve("/usr/bin/getconf", 0x7FEF43B27A70, 0x7FEF43B27CE8)  argc = 2
> 1382:   execve("/usr/bin/printf", 0xFA6897198, 0xFA68971E0)  argc = 2
[...]

Even weirder:

$ /usr/xpg4/bin/sh -c 'type printf'
printf is a shell builtin

$ /usr/xpg4/bin/sh -c 'type printf; type printf'
printf is /usr/bin/printf
printf is a tracked alias for /usr/bin/printf

$ /usr/xpg4/bin/sh -c '${0+type} printf'
printf is /usr/bin/printf

$ /usr/xpg4/bin/sh -c '"type" printf'
printf is /usr/bin/printf

$ /usr/xpg4/bin/sh -c 'type "printf"'
printf is /usr/bin/printf

$ cd ~/bin
$ PATH=. /usr/xpg4/bin/sh -c 'type printf'
printf is /export/home/chazelas/bin/printf

$ cd /usr/bin
$ PATH=. /usr/xpg4/bin/sh -c 'type printf'
printf is a shell builtin

$ PATH=$(/usr/xpg4/bin/getconf PATH) /usr/xpg4/bin/sh -c 'type printf'
printf is /usr/bin/printf

That seems to be because its "type" (at least when it's the only
command run in the script) behaves like the "type" found by a
$PATH lookup. And on Solaris 11, /usr/bin/type  reports "printf"
as builtin. It looks like that /usr/bin/type, linked to 17 other
commands that are typically builtins is a special build of ksh93
dedicated to run builtins (it's actually larger than the ksh93
executable):

$ (exec -a eval /usr/bin/type 'echo "${.sh.version}"')
builtin: eval: restricted name
Version AJM 93u+ 2012-08-01

Which explains why printf is reported as builtin when a PATH
lookup of "type" finds /usr/bin/type instead of
/usr/xpg4/bin/type as printf is indeed a ksh93 builtin.

That doesn't explain why it's different with ${0+type} or when
there's more than the one invocation of "type" in the script.

It's not limited to "printf":

$ /usr/xpg4/bin/sh -c 'print -h'
print[9]: print: -h: unknown option
Usage: print [-enprsvC] [-f format] [-u fd] [string ...]
$ /usr/xpg4/bin/sh -c 'print "-h"'
/usr/xpg4/bin/sh: print: bad option(s)


That is completely messed up, and I think POSIX and its
messed-up builtin lookup in $PATH specification is largely to
blame.

-- 
Stephane

Reply via email to