Pete's Linux Advent Calendar 2007

The 17th day

The first argument is $0

When writing programs or scripts you normally parse command line arguments passed to it, in bash you use $* or "$@". But you can also change the behaviour of your script by using $0. Shells use this feature, if the first character of the zeroth argument is a dash (-) it is treated as a login shell. For example I use this to create shortcuts to login to hosts. So I have a script that essentially looks like this:
#!/bin/bash
myname="${0##*/}"
ssh "$@" "$myname"
then I create symbolic links to this scripts named after the hosts I want to log into. Then I only have to type in the name of the host. I still can add command line arguments to ssh. You can also create a link to /bin/bash to modify the name that is seen in the process list. Finally you can run exec -a xlogo /bin/bash to run bash with the name of "xlogo" This of course can be confusing ;)
Pete's Linux Advent Calendar 2007

Valid HTML 4.01!

Comments? send mail to pkruse@arcor.de my monogram