Wednesday, August 13, 2008

Control Keys

Control keys-those that you type by holding down the CONTROL (or CTRL) key and hitting another key-are another
type of special character. These normally don't print anything on your screen, but the operating system interprets a few
of them as special commands. You already know one of them: RETURN is actually the same as [CTRL-M] (try it and
see). You have probably also used the BACKSPACE or DEL key to erase typos on your command line.
Perhaps the most difficult thing about control keys is that they can differ from system to system. The usual arrangement
is shown in Table which lists the control keys that all major modern versions of UNIX support. Note that [CTRL-\] and [CTRL-|] (control-backslash and control-pipe) are the same character notated two different ways; the same is true of DEL and [CTRL-?].
You can use the stty command to find out what your settings are and change them if you wish;
If the version of UNIX on your system is one of those that derive from BSD (such as SunOS and Ultrix), type stty --all or stty -a to see your control-key settings; you will see something like this:
erase kill werase rprnt flush lnext susp intr quit stop eof
^? ^U ^W ^R ^O ^V ^Z/^Y ^C ^| ^S/^Q ^D

Control Key stty Name Function Description
CTRL-C intr Stop current command
CTRL-D eof End of input
[CTRL-\] or [CTRL-|] quit Stop current command, if [CTRL-C] doesn't work
CTRL-S stop Halt output to screen
CTRL-Q Restart output to screen
DEL or [CTRL-?] erase Erase last character
CTRL-U kill Erase entire command line
CTRL-Z susp Suspend current command

The ^X notation stands for CTRL-X. If your UNIX version derives from System III or System V (this includes AIX,
HP/UX, SCO, and Xenix), type stty -a; the resulting output will include this information:
intr = ^c; quit = ^|; erase = DEL; kill = ^u; eof = ^d; eol = ^`; swtch = ^`
susp = ^z; dsusp ;

The control key you will probably use most often is [CTRL-C], sometimes called the interrupt key. This stops-or tries to stop-the command that is currently running. You will want to use this when you enter a command and find that it's taking too long, you gave it the wrong arguments by mistake, you change your mind about wanting to run it, or whatever.

Sometimes [CTRL-C] doesn't work; in that case, if you really want to stop a job, try [CTRL-\]. But don't just type CTRL-\; always try [CTRL-C] first! Chapter 8 explains why in detail. For now, suffice it to say that [CTRL-C] gives the running job more of a chance to clean up before exiting, so that files and other resources are not left in funny states.

We've already seen an example of [CTRL-D]. When you are running a command that accepts standard input from your keyboard, [CTRL-D] tells the process that your input is finished-as if the process were reading a file and it reached the end of the file. mail is a utility in which this happens often. When you are typing in a message, you end by typing [CTRL-D]. This tells mail that your message is complete and ready to be sent. Most utilities that accept standard input
understand [CTRL-D] as the end-of-input character, though many such programs accept commands like q, quit, exit, etc. The shell itself understands [CTRL-D] as the end-of-input character: as we saw earlier in this chapter, you can normally end a login session by typing [CTRL-D] at the shell prompt. You are just telling the shell that its command input is finished.

CTRL-S and [CTRL-Q] are called flow-control characters. They represent an antiquated way of stopping and restarting the flow of output from one device to another (e.g., from the computer to your terminal) that was useful when the speed of such output was low. They are rather obsolete in these days of high-speed local networks and dialup lines. In fact, under the latter conditions, CTRL-S and [CTRL-Q] are basically a nuisance. The only thing you really need to know
about them is that if your screen output becomes "stuck," then you may have hit [CTRL-S] by accident. Type [CTRL-Q] to restart the output; any keys you may have hit in between will then take effect.

The final group of control characters gives you rudimentary ways to edit your command line. DEL acts as a backspace key (in fact, some systems use the actual BACKSPACE or [CTRL-H] key as "erase" instead of DEL); [CTRL-U] erases the entire line and lets you start over. Again, these are outmoded. [13] Instead of using these, go to the next chapter and read about Korn shell's editing modes, which are among its most exciting features.
[13] Why are so many outmoded control keys still in use? They have nothing to do with the shell per se; instead, they are recognized by the tty driver, an old and hoary part of the operating system's lower depths that controls input and output to/from your terminal.

No comments: