UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 45.3 Don't Need a Shell for Your Script?  Don't Use One Chapter 45
Shell Programming for the Initiated
Next: 45.5 A File That Shows Itself... and What #! Does
 

45.4 Fun with #!

[You might think that the "magic" characters #! are only for shell scripts. Not true (45.5, 45.3)! Here are some fun examples. Study them and (if your UNIX system understands #!) try them; I hope they'll help you see what #! really does. -JP]

Q: Why begin a shell script with #!/bin/sh or #!/bin/csh?

A: Under some systems - principally those with Berkeley influence - this makes the program directly executable. That is, the kernel can start this program, even though it's not machine code; the kernel will invoke the named program after fiddling arguments a bit.

In fact, the script:

#! /bin/mv

will rename itself. Place it in a file called zap, and type zap zup, and now you have a shell script called zup. Your shell tried to exec the program with the argument zup. This succeeded, but actually ran /bin/mv with the arguments zap zup.

You can make self-removing scripts:

#! /bin/rm

Or self-printing scripts:

#! /bin/awk NR>1{print}
text...

This last one works because the kernel is willing to do more than insert the filename in the argument list: it will insert an optional argument. Some systems allow only one such argument (which can then contain blanks), while others allow multiple blank (space or TAB) separated arguments. Note that, in the latter case, there is probably no way to embed blanks (the kernel is unlikely to provide shell-like quoting mechanisms); the safest course is to avoid them entirely.

Normally, this is used for things like the -f option to the C shell ("fast", don't read .cshrcs), but it works well enough for awk too.

#! is described, though not completely, in the execve(2) manual page. Note that there may be a small limit on the number of characters in the #! line, typically 32. (32 is "magic" because it equals sizeof(struct exec).)

- CT in net.unix on Usenet, 29 December 1984


Previous: 45.3 Don't Need a Shell for Your Script?  Don't Use One UNIX Power ToolsNext: 45.5 A File That Shows Itself... and What #! Does
45.3 Don't Need a Shell for Your Script? Don't Use One Book Index45.5 A File That Shows Itself... and What #! Does

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