tags:

views:

120

answers:

4

In a C program I can write argv[0] and the new name shows up in a ps listing.

How can I do this in bash?

+2  A: 

You can do it when running a new program via exec -a <newname>.

Ignacio Vazquez-Abrams
+1: and to add to the above, you could have an "if $0 isn't what i want, then 'exec -a what_i_want'" at the top...
eruciform
Doesn't seem to have any affect for me in Bash 4.0.33
Dennis Williamson
Unfortunately it doesn't affect the *current* shell. You have to open a new one using it.
Ignacio Vazquez-Abrams
A: 
( exec -a foo bash -c 'echo $0' ) 
joe
This will *restart* the process. Not the same as writing to argv[0] to change the name.
bstpierre
A: 

I will just add that this must be possible at runtime, at least in some environments. Assigning $0 in perl on linux does change what shows up in ps. I do not know how that is implemented, however. If I can find out, i'll update this.

edit: Based on how perl does it, it is non-trivial. I doubt there is any bask built in way at runtime but don't know for sure. You can see how perl does it here: http://www.google.com/codesearch/phl=en#ekN1pJ28BN4/mg.c&amp;q=perl_magic_set%20%22$0%22&amp;sa=N&amp;cd=1&amp;ct=rc&amp;l=2785

frankc
A: 

I've had a chance to go through the source for bash and it does not look like there is any support for writing to argv[0].

bstpierre