2

Why in interpreted languages the # normally introduces a comment? This question was asked in an exam on Shell Programming but I don't find any hint on why it's the #.

Any ideas?

skaffman
  • 398,947
  • 96
  • 818
  • 769
helpermethod
  • 59,493
  • 71
  • 188
  • 276
  • 3
    Because `--` and `//` and `/* ... */` require more characters to type and `#` isn't ususually used for anything else (such as pointer or mathematical notation)....I'm guessing? – FrustratedWithFormsDesigner Feb 14 '11 at 18:27
  • @Frustrated - why did you post that as a comment, and not an answer? – Andy White Feb 14 '11 at 18:28
  • That's a either stupid (we can't read the language designers' minds and it propably doesn't matter anyway) or obscure (if there's some hidden meaning, which would be *very* hidden). Halve the intensity of this rant if the teacher mentioned this (and not just in a small remark). –  Feb 14 '11 at 18:29
  • 1
    praise the gods it's `#` and not just any character in the first column – Anycorn Feb 14 '11 at 18:30
  • The /* ... */ style comments are more difficult for parsers than a comment introduced by a single character and running to the end of line. But why # rather than other character is not really clear or particularly useful. – Jimmy Feb 14 '11 at 18:34
  • @Andy White: Beacuse I have no idea if that is the REAL reason behind what the language designers were thinking. It was a best-guess, based on what *I* would do. – FrustratedWithFormsDesigner Feb 14 '11 at 20:53

1 Answers1

3

make uses the #-comment construct; and sh, one the first shells, uses the same #-comment. The writers of later shells -- csh, ksh, bash, jsh, and more -- understood that it would be a burden on users if each were to have its own comment convention, particularly since all of these shell scripts can sort-of run under any shell.

To invent yet another comment convention would be to ensure that no one would use any newly-introduced shell.

The #-comment became a de facto standard very early in Unix history.

That's my take, anyway.

-- pete

Pete Wilson
  • 8,610
  • 6
  • 39
  • 51