22.2.3 :

back: magic numbers
forward: functions
fastback: functions
up: sh implementation
fastforward: environment
top: autoconf, automake, and libtool
contents: table of contents
index: index
about: about this document

In the beginning, the magic number for Bourne shell scripts used to be a colon followed by a newline. Most Unices still support this, and will correctly pass a file with a single colon as its first line to `/bin/sh' for interpretation. Nobody uses this any more and I suspect some very new Unices may have forgotten about it entirely, so you should stick to the more usual `#! /bin/sh' syntax for your own scripts. You may occasionally come across a very old script that starts with a `:' though, and it is nice to know why!

In addition, all known Bourne compatible shells have a builtin command, `:' which always returns success. It is equivalent to the system command /bin/true , but can be used from a script without the overhead of starting another process. When setting a shell variable as a flag, it is good practice to use the commands, : and false as values, and choose the sense of the variable to be `:' in the common case: When you come to test the value of the variable, you will avoid the overhead of additional processes most of the time.

 var=: if $var; then   foo fi 

The : command described above can take any number of arguments, which it will fastidiously ignore. This allows the `:' character to double up as a comment leader of sorts. Be aware that the characters that follow are not discarded, they are still interpreted by the shell, so metacharacters can have unexpected effects:

 $ cat foo : : echo foo : `echo bar` : `echo baz >&2' $ ./foo baz 

You may find very old shell scripts that are commented using `:' , or new scripts that exploit this behavior in some esoteric fashion. My advice is, don't: It will bite you later.

This document was generated by Gary V. Vaughan on May, 24 2001 using texi2html

GNU Autoconf, Automake and Libtool
GNU Autoconf, Automake, and Libtool
ISBN: 1578701902
EAN: 2147483647
Year: 2002
Pages: 290

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net