4

In bash at least, if and case blocks are closed like this:

if some-expr
then
    echo "hello world"
fi

case $some-var in
[1-5])
    do-a-thing
    ;;
*)
    do-another-thing
esac

as opposed to the more typical close of end or endif/endcase. As far as I know, this rather funny convention is unique to shell scripting and I have never seen such an odd block terminator anywhere else. Sometimes things like this have an origin in another language (like Ruby's elsif coming from Perl), or a strange justification. Does this feature of shell scripting have a story behind it? Is it found in other languages?

  • It appeared in Algol68 somewhere in the 1960s. I'm not sure who's idea it was. If you dig through http://archive.computerhistory.org/resources/text/algol/algol_bulletin you might be able to spot who. A few other languages do use it, eg http://en.wikipedia.org/wiki/Maple_%28software%29 has it hidden as an alternative. – NevilleDNZ Nov 23 '12 at 06:46

2 Answers2

11

According to Wikipedia, the sh program reused the syntax from ALGOL 68.

Stephen Bourne carried into this shell some aspects of the ALGOL 68C compiler that he had been working on at Cambridge University. Notably he reused portions of ALGOL 68's "if ~ then ~ elif ~ else ~ fi", "case ~ in ~ esac" and "for ~ while ~ do ~ od" (using done instead of od) clauses in the common Unix Bourne shell syntax.

Since Bash was the GNU replacement of sh, it reused the same syntax for backwards compatibility.

Jeremy
  • 4,791
  • 1
  • 25
  • 40
6

No. You find these "reversed" language constructs in many languages:

  • { and },
  • /* and */
  • (* and *)

:)

haylem
  • 28,856
  • 10
  • 103
  • 119