The stty Command Viewing and changing terminal attributes

 

The stty Command
Viewing and changing terminal attributes directly from the shell โ€” Chapter 62
๐Ÿ–ฅ๏ธ Shell Tool
๐Ÿ”ง Debug & Fix
๐ŸŽฏ Interview Q&A

What is stty?

stty is the shell’s version of tcgetattr() and tcsetattr(). Instead of writing C code, you can view and modify all terminal settings directly from the command line.

It is especially useful for:

  • Checking what settings are currently active on your terminal
  • Debugging programs that modify terminal settings
  • Fixing a broken terminal where a program crashed and left the terminal in an unusable state
  • Monitoring and changing settings on a different terminal (with the -F option)

stty operates on the terminal attached to its standard input by default.

Key Terms in This Section
stty stty -a stty sane -F option line discipline baud rate terminal flags control characters TOSTOP

1. Viewing All Terminal Settings โ€” stty -a

Run this command on a virtual console:

$ stty -a

Sample output:

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
isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop

Understanding the stty -a output โ€” section by section
Line 1
speed 38400 baud โ€” line speed (bits per second)
rows 25; columns 80 โ€” terminal window size
line = 0 โ€” line discipline number (0 = N_TTY, the standard line discipline)
Lines 2โ€“4
Special characters โ€” intr=^C means Ctrl+C is the interrupt character. <undef> means that character is not assigned. min and time relate to noncanonical mode (covered in Chapter 62.6.2).
Remaining
Flag settings from (in order): c_cflag, c_iflag, c_oflag, c_lflag.
A hyphen (-) before a name means that flag is currently disabled. No hyphen means it is enabled.
Example: -tostop means TOSTOP is OFF. echo means ECHO is ON.

2. Changing Terminal Special Characters with stty

You can change which key acts as each special character. Three ways to specify a control character:

Method Example
Caret + letter (^X notation) stty intr ^L
Octal number stty intr 014
Hexadecimal number stty intr 0xC

Examples:

# Change interrupt character from ^C to ^L (Control-L)
$ stty intr ^L

# If the character would be interpreted by the shell,
# precede it with the literal-next character (usually ^V):
$ stty intr Control-V Control-L
# (No whitespace between Control-V and Control-L in real typing)

# Unusual: make q the interrupt character
$ stty intr q
# WARNING: q can no longer be typed as a regular letter!

Warning: If you set a special character to a regular printable letter (like ‘q’), that letter loses its normal function. You can no longer type that character in the terminal.

3. Enabling and Disabling Terminal Flags

To enable or disable a flag, use the flag name with or without a minus sign:

# Enable the TOSTOP flag
# (Background processes that try to write to the terminal get SIGTTOU)
$ stty tostop

# Disable the TOSTOP flag
$ stty -tostop

# Disable echo (hide what you type)
$ stty -echo

# Re-enable echo
$ stty echo

4. Fixing a Broken Terminal โ€” stty sane

When you are developing programs that modify terminal attributes, a program may crash and leave the terminal in an unusable state. Common symptoms:

  • You cannot see what you type (echo is off)
  • Pressing Enter has no effect (ICRNL mapping is off)
  • Everything looks garbled

On a terminal emulator, the easiest fix is to close the window and open a new one. But if that is not possible, type this exact sequence:

Control-J stty sane Control-J

Why this specific sequence?
Control-J (first)
ASCII 10 (the real newline character). We use this because in a broken terminal, the Enter key (ASCII 13) may no longer be mapped to a newline. The first Control-J ensures we start on a fresh command line.
stty sane
Resets all terminal flags and special characters to a reasonable default state. You may not see this as you type (echo may be off), but type it anyway.
Control-J (second)
Executes the command (sends a real newline). After this, the terminal should be back to normal.

5. Monitoring Another Terminal โ€” stty -F

By default, stty operates on the terminal attached to its standard input. Using the -F option (Linux-specific), you can inspect or change a different terminal:

# First, become root (needed to access another user's terminal)
$ su
Password:

# Show all attributes for terminal /dev/tty3
# stty -a -F /dev/tty3

# POSIX-compatible alternative (works on Linux and other UNIX systems):
# stty -a < /dev/tty3

Note: The -F option is a Linux extension to stty. On other UNIX systems, input redirection (< /dev/ttyN) is the portable way to target a different terminal.

Running stty without any arguments shows a minimal summary โ€” only the line speed, line discipline, and any settings that differ from sane defaults:

$ stty
speed 38400 baud; line = 0;
-brkint -imaxbel

6. Practical Workflow โ€” Using stty to Debug a Terminal Program

Suppose you wrote a C program that reads terminal settings, modifies them, and may crash mid-way. Here is how to use stty to debug it.

# Step 1: Before running your program, save current settings
$ stty -g > /tmp/terminal_backup.txt

# Step 2: Run your program
$ ./my_program

# Step 3: If it crashes and the terminal looks broken, restore:
$ stty $(cat /tmp/terminal_backup.txt)

# Or just use sane as a quick fix:
Control-J stty sane Control-J

stty -g outputs the current settings in a compact format that can be fed back directly to stty โ€” perfect for save-and-restore workflows.

๐ŸŽฏ Interview Questions & Answers

Q1. What does stty -a show and how is the output structured?

stty -a displays all terminal attribute settings. The output is structured as: first line shows baud rate, window size, and line discipline; the next few lines show special character assignments; the remaining lines show flag settings from c_cflag, c_iflag, c_oflag, and c_lflag. A flag prefixed with a hyphen (-) is disabled; without hyphen it is enabled.

Q2. What is the line discipline shown in stty -a output and what does 0 mean?

The line discipline is a software layer in the terminal driver that handles line editing and special character processing between the hardware and the user process. Value 0 corresponds to N_TTY (New TTY), which is the standard line discipline that handles canonical mode input, echo, signal generation, and other common terminal behaviors.

Q3. A program crashed and your terminal is broken โ€” no echo, Enter doesn’t work. How do you fix it?

Type: Control-J stty sane Control-J. Control-J is the actual ASCII newline (10), which works even when the Enter key (ASCII 13 / carriage return) is not being mapped to newline. The command stty sane resets all terminal flags and special characters to safe defaults.

Q4. How do you view and change terminal settings for a different terminal device?

On Linux, use stty -F /dev/ttyN (Linux-specific extension). The portable POSIX way is to redirect standard input: stty -a < /dev/tty3. Note that accessing another user’s terminal requires root privileges.

Q5. How do you save and restore terminal settings using stty?

Use stty -g to output current settings in a machine-readable format and save it to a variable or file. To restore: stty $(stty -g) or stty $(cat saved_settings.txt). This is the shell equivalent of the C pattern of saving a copy of the termios struct before modification.

Q6. What is TOSTOP and how do you enable it using stty?

TOSTOP (c_lflag) causes a background process that tries to write to the controlling terminal to receive the signal SIGTTOU, which stops it. Enable it with stty tostop. Disable with stty -tostop. When disabled (the default), background processes can write freely to the terminal.

Q7. Why does stty use Control-J and not Enter (Return) when the terminal is broken?

The Enter key sends ASCII 13 (carriage return, ^M). In a broken terminal, the ICRNL flag (which maps carriage return to newline) may be disabled, so the kernel never generates a newline and the command never executes. ASCII 10 (^J, Control-J) is the actual newline character and is processed as-is by the kernel regardless of the ICRNL flag.

Continue Learning

Next: Deep dive into terminal special characters โ€” the c_cc array, VEOF, VINTR, VKILL, and more

Next: Special Characters โ†’ โ† Previous: tcgetattr/tcsetattr

Leave a Reply

Your email address will not be published. Required fields are marked *