3 ms·
I've heard this story before, but I don't think it's true. I think the bourne shell copied # comments from csh, which had them first. When sh first implemented
by taviso 6y ago
I've heard this story before, but I don't think it's true.
I think the bourne shell copied # comments from csh, which had them first. When sh first implemented them, # comments only worked in scripts, not if you typed them at the shell (i.e. if isatty(0), then # wasn't special). The fact that the prompt came before comments, and that comments didn't work interactively really makes the story sound suspect!
I don't know the origin of # in csh, but I would guess it was chosen because it looks like a CPP directive.
- kragen 6y agoHmm, wasn't # the default erase character for teletype-era Unix? And @ the "kill" character (like modern ^U)? That would have made them a little annoying to type—not annoying enough to keep them from being used for cpp, though, and https://github.com/memnoth/unix-v6/blob/master/sys/dmr/tty.c#L30 https://github.com/memnoth/unix-v6/blob/master/sys/dmr/tty.c... suggests that you could type \#. (There was no "lnext" yet; it was just hardcoded to backslash.) The actual input erase handling is on https://github.com/memnoth/unix-v6/blob/master/sys/dmr/tty.c#L220 https://github.com/memnoth/unix-v6/blob/master/sys/dmr/tty.c.... The relevant struct member is declared in https://github.com/memnoth/unix-v6/blob/master/sys/tty.h#L35 https://github.com/memnoth/unix-v6/blob/master/sys/tty.h#L35 and initialized to CERASE in each tty driver, for example, https://github.com/memnoth/unix-v6/blob/master/sys/dmr/kl.c#L60 https://github.com/memnoth/unix-v6/blob/master/sys/dmr/kl.c#..., using the macro defined in https://github.com/memnoth/unix-v6/blob/master/sys/tty.h#L48 https://github.com/memnoth/unix-v6/blob/master/sys/tty.h#L48. So by default # was special, just to the kernel tty driver, not the shell.
- rcarmo 6y agoThat's a fair point. But in the 90s as a System V and SunOS user, I saw some discussions happen on Usenet, and by the time X was commonplace, the myth/rationale was commonplace.
- JdeBP 6y agoEven then, though, that was over a decade after the fact, and people were inventing explanations for things retroactively. Doug McIlroy has put the kibosh on this myth. * https://minnie.tuhs.org/pipermail/tuhs/2017-April/011286.html https://minnie.tuhs.org/pipermail/tuhs/2017-April/011286.htm...
- mjevans 6y agoEven if # isn't / wasn't special, it's still exceedingly unlikely to be a valid program name, nor is it likely for a valid program name to begin with #. The practical effect should be the same as it having been special.
- taviso 6y agoRight, but the story sounds plausible because accidentally pasting '# foo > bar' would safely neuter everything else on the line. If # isn't special, then the rest of the line is still parsed, so pasting '# foo || bar' or '# foo > baz' does do something.
- dfox 6y agoI don't have reference for that but I always though that CPP directives are preceded by # because the character was previously used as comment delimiter by something (probably csh). In the grand scheme of things cpp is somewhat modern invention. IIRC the first book on Unix programming I had read mentioned in passing that cpp exists but the C code examples in there did not #include anything and if some declaration was required it was just written verbatim in the example code.