1

I remember many years (decades) ago using a system where one could say something like:

$ realname:newname A B C

and the program would be invoked with argv = { "newname", "A", "B", "C" }.

I don't remember what shell this was though.

Does such a capability exist in any current shells?

UPDATE:

exec -a looked like it was what I wanted, but I'm getting inconsistent behaviour depending upon the program's language (yes, I know it's awful code):

$ cat argv.c  && cc argv.c
int main(int argc, char **argv) { printf("<< %s >>\n", argv[0]); }

$ (exec -a fake ./a.out)
<< fake >>

It works fine in "C", but not in python:

$ cat argv.py
#!/usr/bin/python3
import sys
print("<<", sys.argv[0], ">>")

$ (exec -a fake ./argv.py)
<< /home/ray/test/argv.py >>
  • @mosvy, that looked like it would work, but didn't quite do it. See my update. – Ray Butterworth Jan 16 at 16:13
  • ‎i‎n‎d‎e‎e‎d‎ ‎ – mosvy Jan 16 at 16:41
2

When starting a shell using its -c option to run an in-line script, the first argument will be placed in $0:

$ sh -c 'printf "0: %s\n" "$0"; printf "Other: %s\n" "$@"'  hello there how are you
0: hello
Other: there
Other: how
Other: are
Other: you

In bash, assigning to BASH_ARGV0 changes the current shell's $0 value:

$ printf '%s\n' "$0"
/usr/local/bin/bash
$ BASH_ARGV0="hello"
$ printf '%s\n' "$0"
hello

(but it does not change the process' name on my system)

Using set, you'd change the other arguments:

$ printf '%s\n' "$@"

$ set -- A B C
$ printf '%s\n' "$@"
A
B
C
| improve this answer | |
0

A symlink maybe enought for most systems:

~$ cat test.c 
#include <stdio.h>

int main(int ac, char **av)
{
    printf("%s\n", av[0]);
}
~$ cc -o test test.c
~$ ./test 
./test
~$ ln -s test coincoin 
~$ ./coincoin 
./coincoin

This is the mechanism used by busybox (widely used in embedded systems) to implement several utilities (ping, route, etc...) using only one binary.

| improve this answer | |
  • 1
    Also common on many systems to do similar things. On OpenBSD, for example, the cksum, md5, sha1, sha256 and sha512 utilities are all the same file with different names (hard links). – Kusalananda Jan 16 at 15:13
0

[not a real answer yet]

When used with a she-banged executable script, neither

execl("/path/to/script", "new_av0", (void*)0)

in C, nor

exec -a new_av0 /path/to/script

in shells which support exec -a will be able to set $0 or sys.argv[0] (or whatever language syntax is used to refer to the zeroth argument) in that script.

When executing the interpreter of a she-banged script, the kernel will irretrievably discard the original zeroth argument, splicing in its place the name of the interpreter, any optional she-bang line argument, and the path to the script.

Any solution is bound to be tricky, limited or language/system dependent.

For instance, a shell script could be fooled with:

sh -c '. /path/to/script' new_av0

But obviously, this will not affect the command line as it appears in the output of ps, /proc/<pid>/cmline, etc.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.