HP-UX HB v13.00 Ch-11 - Software Development

HP-UX Handbook Rev 13.00 Page 47 (of 101)
Chapter 11 Software Development
October 29, 2013
The Interpreter Line
Scripts cannot be executed directly by the system. They require an interpreter to read and execute
the commands contained in the script. The regular way to execute a script is to pass the script
name as an argument to its interpreter, e.g.
$ /usr/bin/ksh myscript.sh
But if the script file has execute permission, it can also be started by typing just the name of the
script:
$ ./myscript.sh
This is a feature of exec(2). It tries to determine the type of the file via its magic(4) number.
Every executable type has a specific magic number, and scripts can have one too. If the first two
characters of the script file are "#!", exec(2) recognizes them as the magic number for a script
and expects the interpreter name, one optional argument and a newline character to follow, e.g.
#! /usr/bin/ksh
This is the so called interpreter line. Instead of executing the script, the interpreter is executed
and the script name is passed to the interpreter as an argument. Any executable can be specified
in the interpreter line. You could e.g. use this feature to create a self-printing readme file:
$ cat readme.sh
#! /usr/bin/tail +2
Hello
$ ./readme.sh
Hello
$
If a script has no interpreter line, it cannot be recognized by exec(2). Then it is assumed to be a
shell script, and a POSIX shell will be started to execute it. In general, exec(2) will pass every
ascii file that has execute permissions, but no valid magic number, to the POSIX shell. It is then
up to the shell to try and interpret it, or to stop with an error message.