Re: shebanged script not executed right - wth

From: Richard M Kreuter <kreuter_at_progn.net>
Date: Sun, 10 Aug 2025 02:28:04 UTC
Dag-Erling Smørgrav <des@FreeBSD.org> wrote:
> Richard M Kreuter <kreuter@progn.net> writes:
> > Could you share how you're testing for this behavior?
> 
> I just created three scripts.

Here's how I tried to emulate the OP's report. It seems consistent with
the idea that a script cannot be a shebang interpreter. But I must be
mistaken, as you've found otherwise. What am I missing or
misunderstanding?

--
$ freebsd-version -urk
14.3-RELEASE
14.3-RELEASE
14.3-RELEASE-p1
$ pwd
/home/me
$ cat > script1
#!/bin/sh
exec /usr/local/bin/python3.11 "$@"
^D
$ cat > script2
#!/home/me/script1
print("foo")
^D
$ chmod 755 script1 script2
# Verify that script2 works
$ python3.11 script2
foo
# Verify that script1 works
$ ./script1 script2
foo
# But the shebang line doesn't work
$ ./script2
./script2: 2: Syntax error: word unexpected (expecting ")")
# And for clarity, that error comes from the shell:
$ sh -c 'print("foo")'
sh: Syntax error: word unexpected (expecting ")")
# Now let's see what's really going on
$ truss -a sh -c './script2'
--

It seems to me the relevant lines from truss are:

--
execve("./script2",[ "./script2" ],0x2a7626840208) ERR#8 'Exec format error'
openat(AT_FDCWD,"./script2",O_RDONLY|O_NONBLOCK,00) = 3 (0x3)
pread(3,"#!/home/me/script1\nprint(""...,256,0x0) = 38 (0x26)
close(3)					 = 0 (0x0)
execve("/bin/sh",[ "/bin/sh", "./script2" ],0x2a7626840208) EJUSTRETURN
--

The failure of the first execve is consistent with the theory that a
script cannot be a shebang interpreter. So I'm not sure how to reconcile
that with your finding.

Thanks in advance,
Richard