stty, tty, clear & reset

The 4 commands above apply to terminal sessions only.

The stty command lets us determine the terminal settings currently in use…

root:/tmp> stty ­-a
speed 38400 baud; rows 62; columns 171; line = 0;
intr = ^C; quit = ^; erase = ^?; kill = ^U; eof = ^D; eol = M­-^?; eol2 = M-­^?; swtch = M­-^?;
start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W; lnext = ^V;
flush = ^O; min = 1; time = 0;
-parenb ­-parodd -­cmspar cs8 hupcl ­-cstopb cread ­-clocal ­-crtscts
-ignbrk brkint -­ignpar ­-parmrk -­inpck ­-istrip -­inlcr ­-igncr -icrnl ixon ­-ixoff ­-iuclc ixany
imaxbel iutf8
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

… as well as change whatever needs changing. As we see above, there are many parameters and options we can tweak but we will rarely need to change most of them. Some of the most common parameters that we would need to know about are:

intr          SIGINT signal is usually triggered by Control+C
quit         SIGQUIT signal is usually triggered by Control+
erase      erase action is usually mapped to the Backspace key
kill          SIGKILL signal is usually triggered by Control+U
eof          end­of­feed or end­of­input is usually triggered by Control+D
susp       SIGSTOP signal is usually triggered by Control+Z
start        re­starting stopped output is usually achieved with Control+Q
stop        stopping output is usually achieved with Control+S

# following settings are disabled with “­” prefix
ixon          enables/disables start/stop output keys
isign         enables/disables intr/quit/susp keys
icanon      enables/disables erase/kill/werase/rprnt keys
echo         enabled by default as we want to see what we type but might be disabled (password input?)
tostop      disabled by default. When enabled stops background processes trying to write to terminal

Let’s set a few common terminal settings to get a grip on this:

root:~> stty erase <Backspace>      → resets the mapping of erase to the Backspace key
root:~> stty quit <Control+Q>       → SIGQUIT will be triggered by Control+Q
root:~> stty -icanon                → disable erase/kill/werase/rprnt keys

If the settings are messed up and we want to reset them all to their default values we can do so with:

root:~> stty sane

For a thorough explanation of all the parameters and options run: “info coreutils ‘stty invocation‘ ” .

The tty command does not have much to talk about, it simply shows us our own terminal device…

root:~> tty
/dev/pts/0

… but it can be used from within a script to determine whether or not stdout is a terminal:

root:~> tty ­-s
root:~> echo $?
0

Zero means stdout is the terminal and one means it isn’t.

When we have the terminal full of output and we want it totally clean and empty (apart from prompt) we just need to execute clear without any arguments and that will do the trick.

If the terminal is misconfigured or its character set has accidentally changed (cating a binary file sometimes does that…), we can reset all the defaults and get a clean terminal again with the reset command.

<< bg, fg, &, jobs, nohup & disown          time, timeout, sleep & usleep >>