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


© December 2003 Tony Lawrence

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.

(OLDER)    <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

-> argv0

Inexpensive and informative Apple related e-books:

Take Control of Numbers

Take Control of Upgrading to El Capitan

Take Control of Parallels Desktop 12

Take Control of the Mac Command Line with Terminal, Second Edition

Photos for Mac: A Take Control Crash Course

More Articles by © Tony Lawrence

Printer Friendly Version

Have you tried Searching this site?

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

Printer Friendly Version

The danger of computers becoming like humans is not as great as the danger of humans becoming like computers. (Konrad Zuse)

Linux posts

Troubleshooting posts

This post tagged:




Unix/Linux Consultants

Skills Tests

Unix/Linux Book Reviews

My Unix/Linux Troubleshooting Book

This site runs on Linode