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

argv0

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
./t
 

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[]) {
        printf("%s\n",argv[0]);
        }
 

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?

Unix/Linux/Mac OS X support by phone, email or on-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





There is no programming language, no matter how structured, that will prevent programmers from making bad programs. (Larry Flon)

If Linux can skate by the patent and copyright issues, its growth in the corporate world will continue no matter what business dislikes about the GPL. (Tony Lawrence)







This post tagged: