------------------------- Comments in Shell Scripts ------------------------- Often, I want to put an example of how to run a shell script inside the shell script as a comment. I might create a script called "doexec" and write: #!/bin/sh -u # # doexec files... # # This script sets execute permissions on all its arguments. if chmod +x "$@" ; then exit 0 else echo "$0: Could not change mode of: $*" exit 1 fi I would execute this script by typing: $ ./doexec filename1 filename2 filename3... This comment line in the doexec script: # doexec files... tells me that the script name is "doexec" and the files are the arguments to the script. But what if I rename the script to be something other than "doexec"? $ mv doexec doex $ ./doex foo bar The use of "$0" in the echo line for the error message ensures that the shell will print the actual script name in the error message, but the comment in the script is now wrong, since the program name is no longer "doexec". I don't want to have to edit the script every time I change the name of the script. The solution is never to put the actual name of a script inside the script, even as a comment. Wherever you refer to the name of the script, even in a comment, use "$0" instead. So, the comment changes from: # doexec files... to be: # $0 files... In the comment, "$0" just means "whatever the name of this script is", without my having to actually write the script name. I don't want to use the actual script name, because I might change it. Since the line is a comment, ignored by the shell, the shell will never actually expand that "$0" to be the real name of the shell; it's just a convenient way of specifying the program name without actually naming it.