4

I want to exec a program and control it's arguments including arg0 and environment. Using C I could go for execve. Can I do this in POSIX shell?

1
  • 3
    Depending on the situation, you could make a symlink with the desired arg0 and exec that instead
    – muru
    Mar 16 at 3:57

1 Answer 1

3

According to POSIX documentation of Special Builtin Utilities, the syntax for exec is

exec [command [argument...]]

Environment of the calling shell should be propagated to command so setting it prior to exec-ing like foo=bar exec cmd should just work. However, you might want to consider the Rationale paragraph.

Most historical implementations were not conformant in that: foo=bar exec cmd did not pass foo to cmd.

There is also no built-in way to fiddle argv[0].
a possible workaround could consist in symlinking some_name to command prior to exec some_name (full credit to muru)

Note that bash zsh and ksh do support an extended implementation of the exec command :

exec [-cl] [-a name] [command [arguments]]

which, thanks, to the -a option enables name to stand as argv[0]

with -a argv0 set the argv[0] string of the command executed

However, neither csh nor dash nor fish support that.

1
  • A symlink won't always do the trick, because the path component may often be different from the intended argv[0], although the basename can be the same.
    – jrw32982
    Mar 21 at 2:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .