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.
stty [-a | -g] stty SETTING... stty -F DEVICE SETTING...
Try it
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.
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
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 andtrap 'stty "$old"' EXITso a crash orCtrl+Cdoes not leave the terminal without echo. If it happens anyway, typestty sane(and pressCtrl+Jinstead of Enter, since Enter may no longer work) or runreset. - Prefer
read -sfor passwords andread -n 1for a single key in Bash; they change the terminal mode for you and put it back. Usesttywhen you need finer control or when the shell has no suchreadoptions. - Terminal size is stored per terminal.
stty sizereads it; the variables$LINESand$COLUMNS(andtput lines,tput cols) report the same numbers. A resizing window sendsSIGWINCH. - Ctrl+S freezes the screen when
ixonis on (it stops output untilCtrl+Q).stty -ixonfrees the key for programs such as Vim or Bash's history search. - Serial devices:
stty -F /dev/ttyUSB0 115200 cs8 -parenb -cstopb raw -echois a typical setup for a microcontroller. You need permission on the device (groupdialouton 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
-avaries slightly between versions; do not parse it. Usestty sizeortputfor values you need in a script. - Not for pipes: if stdin is redirected, test
[ -t 0 ]first (seetty) or point at the terminal explicitly withstty < /dev/tty.
Related
tty,tput,clearandreset,read- Lessons: Reading input, Traps and signals