Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท System Information

Linux stty command

Reference for the Linux stty command: show and change terminal line settings such as echo, size, erase and interrupt keys, and save and restore state in scripts.

stty shows and changes the settings of a terminal line: whether typed characters are echoed, whether input is delivered line by line or key by key, which keys send interrupt (Ctrl+C) or end-of-file (Ctrl+D), how many rows and columns the terminal has, and the baud rate of serial lines.

The name stands for "set tty". Most of the time the terminal is configured for you; you reach for stty to turn off echo while reading a password, to repair a terminal that is garbled after a crashed program (stty sane), or to talk to serial devices.

stty works on the terminal connected to its standard input. In the sandbox here there is no terminal, so the commands that need one fail with Inappropriate ioctl for device; the examples show this and the safe way to write scripts around it.

Terminal
stty [-a | -g]
stty SETTING...
stty -F DEVICE SETTING...

Try it

Bash What happens without a terminal

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Read-only: the password prompt pattern

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
stty [-F DEVICE] [-a | -g] [SETTING]...
Option / setting Description
-a, --all Print all current settings in a readable form.
-g, --save Print the settings in a compact form that stty accepts back as input.
-F DEVICE, --file=DEVICE Use another device than standard input, e.g. -F /dev/ttyUSB0.
size Print rows columns.
rows N, cols N Set the terminal size recorded by the kernel.
echo / -echo Turn echoing of typed characters on / off.
icanon / -icanon Line-by-line (canonical) input on / off. Off delivers each key at once.
isig / -isig Interpret Ctrl+C, Ctrl+Z, Ctrl+\ as signals / pass them through.
raw / -raw, cooked Disable / restore most input and output processing.
sane Reset to reasonable defaults; the repair command for a broken terminal.
erase ^H, intr ^C, eof ^D, kill ^U Change the special characters. Written as ^ plus a letter.
min N, time N With -icanon: wait for at least N characters or a timeout (tenths of a second).
9600, 115200 Set the baud rate (serial lines).
ixon / -ixon Enable / disable Ctrl+S and Ctrl+Q flow control.

Returns: 0 on success; 1 on errors such as invalid argument or stty: 'standard input': Inappropriate ioctl for device when stdin is not a terminal.

Usage notes

  • Always restore. Use old=$(stty -g) first and trap 'stty "$old"' EXIT so a crash or Ctrl+C does not leave the terminal without echo. If it happens anyway, type stty sane (and press Ctrl+J instead of Enter, since Enter may no longer work) or run reset.
  • Prefer read -s for passwords and read -n 1 for a single key in Bash; they change the terminal mode for you and put it back. Use stty when you need finer control or when the shell has no such read options.
  • Terminal size is stored per terminal. stty size reads it; the variables $LINES and $COLUMNS (and tput lines, tput cols) report the same numbers. A resizing window sends SIGWINCH.
  • Ctrl+S freezes the screen when ixon is on (it stops output until Ctrl+Q). stty -ixon frees the key for programs such as Vim or Bash's history search.
  • Serial devices: stty -F /dev/ttyUSB0 115200 cs8 -parenb -cstopb raw -echo is a typical setup for a microcontroller. You need permission on the device (group dialout on many systems).
  • Only the terminal's own side is changed. Applications that use their own line editing (readline, ncurses) set their own mode and restore it on exit.
  • Output of -a varies slightly between versions; do not parse it. Use stty size or tput for values you need in a script.
  • Not for pipes: if stdin is redirected, test [ -t 0 ] first (see tty) or point at the terminal explicitly with stty < /dev/tty.