Tuesday, November 15, 2005

Shebang!

The quickest way to make progress in bash is to bounce back and forth between using it interactivly and non-interactively.

Before we do that, though, it's worth our taking a quick tour of some things you'll see in shell scripts (which is what shell programs are called).

To cut down on confusion, most scripts we show on the tour will do the same thing as yesterday's: print "hello, world." (It was that or "Just Another Perl Hacker" :-)

Here's the second version of yesterday's script.

#!/bin/bash

# "#!" is prononced "shebang" (shell bang)
#
# When a program starts like this,
# the Unix kernel treats it specially,
# and feeds it to a particular interpreter -- in this case, /bin/bash.
#
# When the kernel sends it to bash, bash treats that line as a comment,
# because it starts with a '#'

echo hello, world

# If you don't have bash, use ksh. If you don't have either, us /bin/sh

Cut-and-paste this code into a file, make it executable, and run it. ("Cut-and-paste" is also colloquially known as "snarf-n-barf.")

Try modifying the shebang line to have the kernel send it to other interpreters, like /bin/ksh, /bin/sh, /bin/tcsh, or even /bin/true. What happens?

Claims that "shebang" was originally spelled "Sí bheang" are ill-founded, urban blarney.

0 Comments:

Post a Comment

<< Home