fe...@palmen-it.de (Felix Palmen):
> * Helmut Waitzmann <
nn.th...@xoxy.net>:
>>
fe...@palmen-it.de (Felix Palmen):
>>> The exec() family of functions allows to specify argv[] and
>>> the actual executable separately, and this is what happens
>>> when executing a script that has a shebang. The binary named
>>> in the shebang is executed, but it gets fed the original
>>> commandline in argv[], including the original argv[0].
>>
>> At least, my Debian 10 Linux apparently doesn't follow that
>> part of the last sentence that begins with "but it gets fed":
>> If I
>> [...]
>> I get the following output:
>>
>>
>> PPID SID PGID PID STAT argv
>> 10075 10075 11141 11141 S+ /bin/sh ./show_args Hello, world!
>
> Well, then I was wrong on that one... happens :o so it's always
> the shell "shfiting" argv[] by one when running as an
> interpreter.
The shell examines its argument list, detects, which argument
names the shell script to be read and interpreted, and puts the
value of that argument into the positional parameter "$0". That
argument doesn't necessarily have to be the argv[1] invocation
argument. For example, when running a shell script like
sh -x -v - ./show_args 'Hello,' 'world!'
(i. e. with command echoing and tracing enabled and the end of
options explicitly marked) argv[4] will be the name of the script
file "./show_args", and that is what will be available as "$0"
inside of the script.
This mechanism of copying the shell script's name into "$0" is
done by the invoked shell, even if the shell script does not
start with a "#!" line.
As far as I understand it, a member of the "exec" family of
functions, when given a shell script file name as the first
("path" or "file", see the manual page "exec(3)") parameter, for
example
execlp("the_shell_script", "two", "parameters", 0)
(if the variant of the "exec" family function is a "exec…p"
variant and the "the_shell_script" parameter doesn't contain a
slash ("/"), searches for the "the_shell_script" file according
to the "PATH" environment variable, replaces the name by the full
path name of the script file found, for example
"/path/to/the_shell_script"), opens that file for reading, reads
the pathname of the shell to be invoked and an optional parameter
from the
#! /name/of/the/shell optional_parameter
line, and invokes the shell given in that "#!" line (together
with the optional parameter if given) followed by the shell
script pathname and the remaining parameters, like
execl("/name/of/the/shell", "optional_parameter",
"/path/to/the_shell_script", "two", "parameters", 0)