1

By default bash passes executable filename as first (0 to be more precise) argument while invoking programs

Is there any special form for calling programs that can be used to pass 0 argument?

It is usefull for bunch of programs that behave in different ways depending on location where they were called from

ayvango
  • 5,867
  • 3
  • 34
  • 73

1 Answers1

3

I think the only way to set argument 0 is to change the name of the executable. For example:

$ echo 'echo $0' > foo.sh
$ ln foo.sh bar.sh
$ sh foo.sh
foo.sh
$ sh bar.sh
bar.sh

Some shells have a non-POSIX extension to the exec command that allow you to specify an alternate value:

$ exec -a specialshell bash
$ echo $0
specialshell

I'm not aware of a similar technique for changing the name of a child process like this, other than to run in a subshell

$ ( exec -a subshell-bash bash )

Update: three seconds later, I find the argv0 command at http://cr.yp.to/ucspi-tcp/argv0.html.

chepner
  • 497,756
  • 71
  • 530
  • 681
  • the first methos is not suitable since file with same name already exists (it is the bash script itself :). argv0 command is not found on my system. But there is bash and -a working for it. There is only need in method of brancing exec in different subprocess that I can find easily (I hope). Thanks – ayvango Mar 19 '13 at 17:15