[Chapter 45] 45.6 Making Sure Your Script Runs with Bourne Shell, Without #!
Author: Jerry Peek, Tim O'Reilly & Mike Loukides
Date: 1998-08-04T21:54:25Z

#

| Chapter 45 Shell Programming for the Initiated |

## 45.6 Making Sure Your Script Runs with Bourne Shell, Without #!

Lots of UNIX versions let you start a script file this way:

>     #!/bin/sh

That executable file will always be read by a Bourne shell. If some versions of UNIX you use don't understand `#!` (44.4), here's how to start your scripts:

    ||
    #!/bin/sh
    export PATH || exec /bin/sh $0 $argv:q

If a Bourne shell reads that line (that is, if the `#!/bin/sh` succeeded), the `export` `PATH` command will succeed and the rest of the command line will be skipped. If a C shell reads the line, it will print the error `export: Command not found`. Then it will run `exec /bin/sh $0 $argv:q`. The exec (45.7) replaces the C shell with a Bourne shell, passes it the name of the script file in `$0`, and passes a quoted list of the command-line arguments from `$argv` `:q` (9.6).

- JP

45.5 A File That Shows Itself... and What #! Does |  | 45.7 The exec Command