Tuesday, May 12, 2009

Shell Programming Texts

There are no brief but substantial shell-programming texts.

There are two, alternative models for programming textbooks: K&R, and almost any Java book you pick up. The Java books are boat anchors. They're comprehensive, verbose, detailed, and replete with extensive terminology. K&R is brief, to-the-point.

Shell-programming books seem to follow the Java model. What would a K&R-style shell book look like?

I can't write as well as Brian Kernighan or Dennis Ritchie, but let's take a shot at a few pieces.

Beginnings.

Unlike languages like Java or C, talking to the shell is programming in the shell. Just open a terminal window and type:

$ echo hello, world
hello, world
(Please try all the examples, even if you think you understand them. It's easy enough, so why not?)

You could type this a few times, for practice, but it's easier to recall the command than to retype it. Press the up-arrow once and you'll see the last command reappear. Press enter and the shell will execute the command.
$ ^
$ echo hello, world
The dollar sign is the prompt. It says the shell is ready to listen to your next command. Until you see the dollar sign, it's not. You can type while waiting for a prompt, and the terminal will buffer up your typing to give to the shell when it's finally done with what it's doing and ready for your next command.

A command will normally only be executed after you press ENTER, whether you've typed it or are just recalling it.
$ sleep 10
echo hello, world
[ten seconds elapses, then the next line appears, along with the execution of your command.]
$ echo hello, world
hello
You can press the up arrow a few times to look back through your history, or you can just type the command history.
$ history
1 echo hello, world
2 echo hello, world
3 sleep 10
4 echo hello, world
(Now that you've typed it, the command history is in your history.
$ history
1 echo hello, world
2 echo hello, world
3 sleep 10
4 echo hello, world
5 history
)

You can refer to and re-execute a command by its history number, but we'd like to discourage that. There are more powerful ways to use history, and you should build the habit of using them from the start.


Type control-R (^R), then, slowly, "hel" and watch what happens.
(reverse-i-search)`':
(reverse-i-search)`h': history
(reverse-i-search)`he': echo hello, world
(reverse-i-search)`hel': echo hello, world
The shell is looking, in reverse, through your history to find a command that matches the string you're giving it. This is incremental search. 'h' matched "history", so that's what it found first. As soon as you typed the 'e' , it looked further and found "echo hello, world" The "l" still matched, so it didn't look for a new line.

As soon as you've found the history line you want, press ENTER to execute it.

Exercise 1-1: Search for a command that isn't in your history.

If you find yourself marooned on a line that you do not want to execute, just press ^C to cancel it and get back to a prompt. Try this both while you're typing and while you're searching through your history. Notice, by examining your history when you do this, that canceled commands don't go into your history. Only executed commands are recorded.

No comments: