30 April 2013

History of #!/bin/sh (the 'hashbang' or 'shebang')

I was just reading a message in the XKCD forums, and noticed there were all these comments about how the "#!" was 'interpreted by the shell'. Given that XKCD readers are pretty technical, this surprised me.

Actually, the hashbang is interpreted by the kernel. hashbang was invented in Version 7 UNIX, as a result of the split between East Coast UNIX and West Coast UNIX.

In Version 6 UNIX there was only one shell. It was a pretty horrible shell, too. It didn't have control structures. It didn't have comments. It didn't have file name expansion or wildcards - they were sort of transparently handled by having the shell run a separate program called "glob". You couldn't pipe into shell scripts because the shell interpreted scripts by reading them from standard input. There was no buffering, so it was possible to control the flow of a shell script by seeing around on standard input, looking for a line starting with ":" to indicate a label. ":" wasn't a comment marker, mind, it was a command that didn't do anything, like /bin/true. It may have even been a link to /bin/true.

On the east coast, this fellow at AT&T named Steve Bourne was coming up with a new shell that would end up in Version 7 UNIX.

On the west coast, there were two competing improved shells, the pascal shell (which never took off) and the C shell.

The C shell had control structures and comments and all kinds of cool stuff. The comment character was "#". So when you ran a shell script it forked, opened the file, and looked at the first character, and if it was "#" it execed a new instance of the C shell to interpret it, otherwise it just execed the old /bin/sh with stdin set to the file. OK, that's a lot of jargon, let's just say that the first character was used by the C shell to determine what shell to use to run the script.

Now I'm going to digress into more jargon. When the kernel ran a program, it used the first two bytes of the program to determine how to run it. Originally it just loaded the program and jumped into location zero, but by the time of V6 and V7 there were a number of different things that had to be done to set up the program, so it opened up the file and read the first two bytes. If the first two bytes were a jump to one particular location, it knew it was one kind of program, if it was a jump to another location, it was another kind of program. So this jump instruction became a "magic number" that the kernel used to decide how to run the program. Other magic numbers got created, and they didn't have to be PDP-11 jump instructions, (or even run on a PDP-11) but one number was as good as any other so those just happened to be kept.

Anyway, by this time in UNIX the first 16 bits of the program was a magic number... the kernel read it and used that to decide how to run a program. I could go into more details like "pure text" versus "split I&D", but it doesn't matter... the point is, the feature was there.

So... in Version 7, they defined a new 16-bit magic number whose value looked like "#!" and said "this magic number is interpreted by reading a line of text, breaking it up into two words separated by a space, and calling an interpreter whose name was in the first word, and passing the second word and the name of the file to that interpreter".

This was the hashbang born. Like many great ideas it was simple, brilliant, and extraordinarily useful.

16 March 2013

Lost in Non-Translation

I was just referred to this article by Aaron Diaz (creator of Dresden Codak).

One of Isaac Asimov's most important essays is entitled "Lost in Non-Translation", published in the unfortunately out-of print collection "The Tragedy of the Moon". It points out that simply presenting stories and parables in a historical context robs them of the power that they had for people for whom that context wasn't history.

http://www.asimovreviews.net/Books/Book144.html

http://www.amazon.com/The-Tragedy-Moon-Isaac-Asimov/dp/0440189993

Sorry, folks, you'll have to get it in paper form, it's not available digitally. It's a shame, this book contains some of his best non-fiction works. The other books containing this essay are also out-of-print and paper-only.

(in an ideal world Baen would have the rights and it would be in the Free Library, I consider this essay that important)

26 February 2013

The Possibly Proper Death Litany

Insofar as I may be heard by anything, which may or may not care what I say, I ask, if it matters, that you be forgiven for anything you may have done or failed to do which requires forgiveness. Conversely, if not forgiveness but something else may be required to ensure any possible benefit for which you may be eligible after the destruction of your body, I ask that this, whatever it may be, be granted or withheld, as the case may be, in such a manner as to insure your receiving said benefit. I ask this in my capacity as your elected intermediary between yourself and that which may not be yourself, but which may have an interest in the matter of your receiving as much as it is possible for you to receive of this thing, and which may in some way be influenced by this ceremony. Amen.

23 January 2013

Dear Red Hat:

Why isn't the BSD license listed on this page?

http://www.redhat.com/legal/patent_policy.html

25 November 2012

Open Source on the Windows App Store

I'm seeing a bunch of articles about the Windows App store being somehow more open source friendly than the Apple App Store, because their license explicitly states something that is true in both cases - that a developer's license can override the app store license.

In practical terms, this is meaningless. If you have an ARM-based Windows or iOS device, you can not side-load an open source application into your tablet or phone. This renders the "open source" nature of the license practically irrelevant, and if publishing a GPL app through either store isn't actually a violation of the GPL it's at least a cynical exercise in Tivoization by proxy.

But what really boggles my mind is that people are arguing that this makes Microsoft more "open source friendly" than Apple. Until I can go to opensource.microsoft.com and download some kind of Windows Core kernel, the way I can go to opensource.apple.com and download a Darwin kernel, the idea that Microsoft's open source support is anywhere near Apple's is ludicrous. Sure, their sources are incomplete - they don't include the GUI sources and some components that would make it trivial to run OS X on non-Apple hardware - but they've kept these sources updated even when people have taken them and used them to create OSX86. I really expected Darwin to vanish when that happened - there's no license requirement for it.

05 September 2012

The last sign...

Reported in Oregon, 1986:

Farewell O verse
Along the road
How sad to see
You're out of mode
Burma Shave

02 July 2012

Maybe you just predicted wrong, Cringe?

In Cringely's latest article he declares that the PC is about to become something unrecognizably different. Because that's what he predicted in 1992 would have happened by now, and anyway that's what happens to technology after about 30 years. That's why the telephone of today is unrecognizably different from the telephone of 1900, the movie from the movies of the 1920s, and television from the televisions of the 1950s.