bash


while (/bin/true) do
  echo "Hello, World\!"
done

submitted by: garrett@acm.com (Don Garrett)


I would like to point out that the current example for BASH is, while correct, less than exemplary.

In order to better display the advantages bash has over csh-derived shells in command-line usage, I would write this as:

while true; do echo 'Hello, World!'; done

This takes advantage of the fact that true is in the user's path, reveals a major advantage bash has over csh (the ability to write one-line loop constructs which are much easier to recall and edit through the command-line history), and uses quotes which can quote the history substitution (!) without the aid of a \.

Of course, if saving characters is the objective, we can write

while true;do echo Hello, World\!;done

And if you want to show off the shell's superior programming capabilities we can get silly:

while true; do echo a; done | sed -e 's/a/Hello World!/'

Of course, if your purpose is readability, rather than jihad, I'd go with a compromise version that has the gratuitous whitespace:

while true; do
   echo 'Hello, World!'
done

You can keep the /bin/true if you wish to illustrate the ability of the while construct to execute an arbitrary pipeline. Or you can go overboard...

while echo blah | grep blah > /dev/null; do
   echo 'Hello, World!'
done

Or you can just

while echo 'Hello, World!' ; do
    true
done

since echo has returned true every time I ever used it. It also has a certain "je ne cest HUH?" when juxtaposed with my first suggestion.

I'm sure I could think up a few more twisted examples, but then whatever point I have left would get buried even deeper.


submitted by: thoth@purplefrog.com (Bob Forsman)