If you're writing:
several programs that do the same kinds of things,
programs that use a lot of the same code (as you're writing the second,
third, etc., programs, you copy a lot of lines from the first program), or
a program with several options that make a big change in the way it works,
you might want to write just one program and make
to it instead.
The program can find the name you called it with and, through
commands, work in different ways.
For instance, the Berkeley UNIX commands
, and others are all links to the same executable file.
This takes less disk space and makes maintenance easier.
It's usually only sensible when most of the code is the same in each
If the program is full of name tests and lots of separate code,
this technique may be more trouble than it's worth.
Depending on how the script program is called, this name can be
relative pathname like
-it can also be an
absolute pathname like
There are a couple of ways to handle this in a shell script.
If there's just one main piece of code in the script, as in the
might be best.
The asterisk (
) wildcard at the start of each case
different pathnames that might be used to call the script:
case "$0" in
...do this when called as name1...
...do this when called as name2...
...print error and exit if $0 doesn't match...
You might also want to use
to strip off any leading pathname and store the cleaned-up
You can test
anywhere in the script and also use it for error
case "$myname" in
echo "$myname: aborting; error in xxxxxx" 1>&2