UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 45.5 A File That Shows Itself... and What #! Does Chapter 45
Shell Programming for the Initiated
Next: 45.7 The exec Command
 

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


Previous: 45.5 A File That Shows Itself... and What #! Does UNIX Power ToolsNext: 45.7 The exec Command
45.5 A File That Shows Itself... and What #! Does Book Index45.7 The exec Command

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System