Reputation: 27684
From zshbuiltins manual,
exec [ -cl ] [ -a argv0 ] simple command Replace the current shell with an external command rather than forking. With -c clear the environment; with -l prepend - to the argv[0] string of the command executed (to simulate a login shell); with -a argv0 set the argv[0] string of the command executed. See the section `Precommand Modifiers'.
I tried using -a
with a simple script but it doesn't seem to work
[balakrishnan@mylap scripts]$ cat printer.sh;echo "-----";cat wrapper.sh
#!/bin/sh
echo $0 $1 $2 $3 $4
-----
#!/bin/zsh
argv0="$0"
exec -a "$argv0" printer.sh
[balakrishnan@mylap scripts]$ wrapper.sh
printer.sh
[balakrishnan@mylap scripts]$
Since I set wrapper.sh
as argv0
, I expect that to be printed when printer.sh
echos $0
. But it still print printer.sh
.
Upvotes: 2
Views: 1191
Reputation: 34863
zsh
is setting argv[0]
correctly, but when /bin/sh
runs to interpret the script, it sets $0
to the name of the script being run, ignoring argv[0]
.
The zsh
man page doesn’t explicitly describe this behaviour, but the bash
manpage does:
ARGUMENTS
If arguments remain after option processing, and neither the
-c
nor the-s
option has been supplied, the first argument is assumed to be the name of a file containing shell commands. Ifbash
is invoked in this fashion,$0
is set to the name of the file, and the positional parame- ters are set to the remaining arguments.
You can see that argv[0]
is being set correctly by running a tiny C program instead of a shell script:
#include <stdio.h>
int main(int argc, char** argv) {
printf("%s\n", argv[0]);
return 0;
}
Upvotes: 1