APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed


I haven't seen this anywhere but Linux, though it wouldn't be hard to implement it on any other platform.

Any unix programmer will probably realize what this program does just from its name. It runs an arbitrary program, but does so setting the first argument to whatever you want it to be.

Ordinarily, the first argument (which a c programmer would call argv[0]) is the name of the program itself. If we make a shell script "t":

echo $*
echo $0

and run it as "./t foo bah":

apl$ ./t foo bah
foo bah

We can't use our shell script with "argv0" because it does a direct exec, but we can compile a simple c program:

/* t.c */
main(int argc,char *argv[]) {

If we compile that ("make t" is sufficient), then "./t" will repond with "./t" as expected, but "argv0 ./t foo" will respond "foo".

So what? Well, some programs react differently depending upon what they see argv0 as. Most shells, if called with a leading "-" ("argv0 csh -csh") will act as a "login shell" (see the man page for any shell).

This is probably most useful when you want to run multiple instances of a program but wish to have an easy way to tell them apart in "ps". It would also make it possible to use "killall" more selectively.

Got something to add? Send me email.

Increase ad revenue 50-250% with Ezoic

More Articles by

Find me on Google+

© Tony Lawrence

Kerio Samepage

Have you tried Searching this site?

Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.

Contact us

A Netscape engineer who shan't be named once passed a pointer to JavaScript, stored it as a string and later passed it back to C, killing 30. (Blake Ross)

This post tagged: