A Bourne shell irritation

August 9, 2006

I was bitten by this today, so I am going to grump about it. Today's irritation with the Bourne shell is that the following is illegal syntax:

echo hi &;

This probably looks peculiar and silly, so let me give a real example that is also illegal:

for i in *watch; do nohup ./$i >/dev/null 2>&1 </dev/null &; done

So is '; ;', and for the same reason: in the Bourne shell, you can't have empty 'simple commands', and both '&' and ';' are command terminators.

(The status of newline is somewhat confusing; the best explanation may be the original V7 sh manpage, which calls it an optional command delimiter. It cannot be a simple command terminator, because that would mean multiple blank lines would be a syntax error.)

This becomes more irritating when you write command lines with multiple backgroundings in them, for example:

foo & bar & baz & wait

Speaking for myself, that makes my eyes bleed. I find it much more readable and easier to write:

foo &; bar &; baz &; wait

(I think a lot of the eye-bleeding is that 'a & b' is very similar to the much more common 'a && b', yet does something radically different.)

I have no idea why Bourne decided to be so nit-picky about this aspect of the shell's grammar, but I suspect it mirrors some bit of Algol.

(As an aside, I note that the original V7 Bourne shell manpage is a marvel of packing a great deal of information into not much space and being reasonably lucid in the process.)

Comments on this page:

From at 2006-08-10 12:10:17:
  foo & bar & baz & wait

Speaking for myself, that makes my eyes bleed. I find it much more readable and easier to write:

  foo &; bar &; baz &; wait

Would this (which has the advantage of being legal) make your eyes bleed more or less than the first line?

   foo &:; bar &:; baz &:; wait

I find that : is a surprisingly useful shell command; I frequently use the idiom

   if foo; then 
       echo "foo failed"
       # ... etc.

particularly when the conditional gets complex enough that the alternative construct foo || echo "foo failed" becomes cumbersome.

--Dan Astoorian

By cks at 2006-08-10 23:30:23:

I suspect that my eyes bleed about as much from 'foo &:; bar &:;' as from the alternative, but I haven't seen it or written it before now so I'm not entirely sure. How it looks certainly doesn't make me want to write it myself.

In general I suspect that one can't really kludge away syntax problems, partly because the workarounds are so clearly kludges and kludges in general are annoying.

Written on 09 August 2006.
« Slashdot's tacit admission of failure
A Bourne shell gotcha: redirection order »

Page tools: View Source, View Normal, Add Comment.
Login: Password:
Atom Syndication: Recent Comments.

Last modified: Wed Aug 9 22:48:03 2006
This dinky wiki is brought to you by the Insane Hackers Guild, Python sub-branch.