Terminal Settings

Terminal Settings

Whenever you work with an application, what you see is governed by a couple of mechanisms. If you have a serial terminal, the flow of data is controlled by the serial line characteristics, including the baud rate, the number of data bits, parity, and so on. One aspect that is often forgotten or even unknown to many users is the terminal characteristics, which are used to control the physical appearance on the screen. However, most of the characteristics still apply, even if you are not connected through a serial terminal.

The reason is that these conventions date back to the time of tele-typewriters. You had a keyboard on one end of the connection connected to a printer that printed out every single character you typed. At that time, it was essential that both ends knew what characteristics the connection had. Even as technology advanced there was still a need to ensure both sides communicated in the exact same way. Since you could not guarantee that the default settings were the same on both ends, you needed a way to change the characteristics so that both ends matched.

As I mentioned elsewhere, the serial line characteristics are initially determined by the gettydefs file. The characteristics are often changed within the users’ startup scripts (.profile, .login, etc.). In addition, you can change them yourself by using the stty command. Rather than jumping to changing them, lets take a look at what our current settings are, which we also do with the stty command. With no arguments, stty might give us something like this:

speed 38400 baud; line = 0; -brkint ixoff -imaxbel -iexten -echoctl

This is pretty straightforward. Settings that are Boolean values (on or off) are listed by themselves if they are on (ixoff) or have a minus sign in front if they are turned off (-brkint). Settings that can take on different values (like the baud rate) appear in two formats: one in which the value simply follows the setting name (speed 38400 baud) and one in which an equal sign is between them (line=0).

In general, if a setting has discrete values, like the baud rate, there is no equal sign. There is only a discrete number of baud rates you could have (i.e., there is no 2678 baud). If the stty setting is for something that could take on “any” value (like the interrupt key), then there is an equal sign. Normally, the interrupt key is something like Ctrl-C or the Delete key. However, it could be the f key or the Down-Arrow or whatever.

This example shows the more “significant” terminal (stty) settings. The top line shows the input and output speed of this terminal, which is 38400. On the second line, we see that sending a break sends an interrupt signal (-brkint).

Setting these values is very straightforward. For Boolean settings (on or off), the syntax is simply

stty <setting>

to turn it on or

stty -<setting>
(note the minus sign in front)

to turn it off.

For example, if I wished to turn on input stripping (in which the character is stripped to 7 bits), the command would look like this:

stty istrip

Settings that require a value have the following syntax:

stty <setting> <value>

So, to set the speed (baud rate) to 19200, the syntax would look like this:

stty speed 19200

To set the interrupt character to Ctrl-C, we would enter

stty intr ^C

Note that ^C is not two separate characters. Instead, when you type it, hold down the Ctrl key and press “c.” In most documentation you will see that the letter appears as capital although you actually press the lowercase letter. Sometimes you want to assign the particular characteristic to just a single key. For example, it is often the case that you want to use the backspace key to send an “erase” character. What the erase character does is tell the system to erase the last character, which is exactly what the backspace is supposed to do. Just like the case where you press the control key and the character, stty settings for single keys are done the same way. For example, you would type “stty erase ” and the press the backspace key (followed by the enter key, or course). What you would see might look like this:

stty erase ^?

The ^? is typically what the backspace key will send (at least that is the visual representation of what the backspace sends). You can get the same result by press CTRL-?.

If the default output does not show the particular characteristic you are looking for, you can use the -a option to show all the characteristics. You might end up with output like this:

speed 38400 baud; rows 25; columns 80; line = 0; intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>; eol2 = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W; lnext = ^V; flush = ^O; min = 1; time = 0; -parenb -parodd cs8 hupcl -cstopb cread -clocal -crtscts -ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon ixoff -iuclc -ixany -imaxbel opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0 isig icanon -iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt -echoctl echoke

Here were see a number of well-known characteristics, such as the baud rate, the numbers of rows and columns, interrupt character, end of file character and so on. Some of which we talked about in the section on working with the system. For details on what the rest of these mean, please see the stty(1L) man-page.

In principle, you can set any key to any one of the terminal characteristics. For example, I could set the interrupt key to be the letter g:

stty intr g

Although this not does make too much sense, it is possible. What does make more sense is to set the characteristic to something fitting for your keyboard. For example, you might be using telnet to move between system. The key sequence that your backspace sends may not be ^? (often it is ^H) and you want to set it accordingly (or the ase is reversed, as we discussed above.)

To save, change, and then restore the original values of your stty settings, use the –g option. This option outputs the stty settings as a strings of hexadecimal values. For example, I might get something like this:

stty -g

500:5:d050d:3b:7f:1c:8:15:4:0:0:0:0:0:1a:11:13:0:0:0:0:0:0:0:0:0

We can run the stty command to get these values and make the changes, then run stty again and use these values as the argument. We don’t have to type in everything manually; we simply take advantage of the fact that variables are expanded by the shell before being passed to the command. You could use this to add an additional password to your system:

echo “Enter your password: \c” oldstty=`stty -g` stty -echo intr ^- read password stty $oldstty

Assign the output of the stty command to the variable old, then change the stty settings so that the characters you input are not echoed to the screen and the interrupt key is disabled (this is done with

stty -echo intr ^-
). Then read a line from the keyboard and reset the stty settings to their old value.