The first line

The first line of a script can identify the interpreter to use.

The first line of many — if not most — shell scripts is in this form:

#! interpreter [arg]

For example, a Korn shell script likely starts with:

#! /bin/sh

The line starts with a #, which indicates a comment, so the line is ignored by the shell processing this script. The initial two characters, #!, aren't important to the shell, but the loader code in procnto recognizes them as an instruction to load the specified interpreter and pass it:

  1. the path to the interpreter
  2. the optional argument specified on the first line of the script
  3. the path to the script
  4. any arguments you pass to the script

For example, if your script is called my_script, and you invoke it as:

./my_script my_arg1 my_arg2 ...

then procnto loads:

interpreter [arg] ./my_script my_arg1 my_arg2 ...
  • The interpreter can't be another #! script.
  • The kernel ignores any setuid and getuid permissions on the script; the child still has the same user and group IDs as its parent. (For more information, see Setuid and setgid in the Working with Files chapter of this guide.)

Some interpreters adjust the list of arguments:

  • ksh removes itself from the arguments
  • gawk changes its own path to be simply gawk
  • perl removes itself and the name of the script from the arguments, and puts the name of the script into the $0 variable

For example, let's look at some simple scripts that echo their own arguments.