A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » C
  • » How to change the argv[0] value

#1 2010-11-12 01:32 AM


How to change the argv[0] value


i would like to know how i can change the argv[0] value in order to execute the  compiled c file with different name.

for instance,

if we have a test.c file and compile it            : gcc test.c -o test

so if we want to execute it, we write            : ./test

i would like  to change the execute name to   :  test

Without using the ./

i am trying this:

int main(int argc, char *argv[])
     argv[0] = "test" ;

but it is not working, although the argv[0] is changed to test. if i try to execute the file i need to write again " ./test " and not " test "

Any help would be appreciated . Thanks !

#2 2010-11-12 03:00 PM


Re: How to change the argv[0] value

i want to execute the file by using the command


and Not


Is this possible ?

#3 2010-11-12 03:15 PM

From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: How to change the argv[0] value

Just add the current working directory to your PATH environment variable. E.g. add


to your ~/.bashrc.


#4 2010-11-12 06:07 PM

From: Boston, MA
Registered: 2002-06-12
Posts: 3,847

Re: How to change the argv[0] value

Even that won't work, if the binary is truly called "test"...  Because, "test" is a shell built-in command, which will take precedence over a binary of that name, even if it's in your $PATH...  Name it something else...

I thought you were originally asking how to make the name of your app as seen in "ps" and such different from how you really called it...  That can be done a few ways: instead of replacing "argv[0]" with a new pointer, you need to actually overwrite the existing string it points at...  Alternatively, you may have a setproctitle() function that does this...  Or, the most portable method is to simply exec*() and supply your own arbitrary argv[0] which will be used (and which is separate from the actual command to execute)...  (Just throwing this out there for anyone else who stumbles across this question, looking for a way to do this...)


  • Index
  • » C
  • » How to change the argv[0] value

Board footer

Powered by FluxBB