Inspect, Save and Safely Change Terminal Settings with stty

Type stty raw without saving your settings first and you can lock yourself out of a working prompt in one command. This guide is a small, reversible workflow for inspecting a terminal, saving its current settings, changing one thing, and restoring the original state. The examples use GNU coreutils 9.4, installed on this machine.

Allow about ten minutes. You need a shell attached to a real terminal or pseudo-terminal. These commands normally affect only the terminal connected to standard input, not the whole system, and do not need sudo. Changing settings can disrupt your current session, so save the state before experimenting.

1. Confirm the command and its terminal

Check which implementation is installed, then confirm the shell has a terminal:

$ stty --version
stty (GNU coreutils) 9.4
$ tty
/dev/pts/3

The device name will differ. If tty prints not a tty, commands that inspect or change terminal modes will usually fail with an ioctl error. Run them from an interactive terminal, or use stty -F DEVICE when you have a specific terminal device and permission to open it.

Checkpoint: continue only when tty identifies the terminal you intend to inspect.

2. Read the current settings

With no arguments, stty prints the baud rate, line discipline where supported, and settings that differ from stty sane. For a full human-readable view, use -a:

$ stty -a
speed 38400 baud; rows 24; columns 80; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>;
...
icanon echo echoe echok ...

Output depends on the kernel, terminal emulator and connection. The displayed echo flag means typed characters are echoed. icanon means input is normally delivered a line at a time, with special characters such as erase and kill handled by the terminal driver. Do not treat the rows, columns or baud rate as universal defaults.

Two focused queries are useful when diagnosing a script or terminal application:

$ stty size
24 80
$ stty speed
38400

size reports rows and columns according to the kernel. A terminal or pseudo-terminal may report 0 0 when no window size has been supplied. That is a property of this terminal, not proof that stty is broken.

3. Save a restorable snapshot

Before changing anything, capture the machine-readable form with -g. Store it in a shell variable for the current session:

$ saved_tty=$(stty -g)
$ printf '%s\n' "$saved_tty"
500:5:bf:8a3b:3:1c:7f:15:4:0:1:0:11:13:1a:0:12:f:17:16:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0

The colon-separated value is opaque and can differ between terminals. Do not edit it or assume that a snapshot from one device belongs to another. -g is meant to be accepted as a later setting, so the recovery command is:

$ stty "$saved_tty"

Keep the variable name free of spaces and quote its expansion. If the session is likely to be closed before recovery, write the value to a protected temporary file rather than leaving a terminal configuration in a shared location. The snapshot can contain control-setting information, so avoid putting it in a public log.

4. Make one controlled change

For a visible but low-risk test, disable character echo:

$ stty -echo
$ printf 'echo is now disabled\n'
echo is now disabled

After this command, ordinary typing will not appear on screen. The shell still receives the input, so do not assume the keyboard has stopped working. This is useful for programs that read input privately, but it can make an interactive shell confusing.

Restore echo immediately, either with the setting's positive form or the saved snapshot:

$ stty echo
$ printf 'echo is enabled again\n'
echo is enabled again
$ stty "$saved_tty"

The final command returns all captured settings, not just echo. A setting beginning with - negates it, so -echo disables echo and echo enables it again. This syntax applies to many flags, but not every setting is a simple on or off switch.

5. Use raw and sane only with a recovery path

raw is a combination setting. It disables canonical processing, echo, signal characters and several input and output transformations, then sets min 1 time 0. It is useful for terminal programs, but running it at a shell prompt can make the shell appear unresponsive:

$ stty raw
$ stty -a
$ stty "$saved_tty"

Warning: do not paste a raw-mode command without also preparing the restore command. If the terminal is already garbled and you still have a working shell, type the restore command carefully. If the current shell cannot accept input, open a second terminal, identify the affected device with tty, and use stty -F DEVICE "$saved_tty" only when you are certain that DEVICE is the affected terminal.

sane applies a broad conventional set of settings and resets special characters to their defaults. It can make a damaged terminal usable again, but it is not an undo operation: it does not restore your personal or application-specific configuration. Use it as recovery when no trustworthy snapshot exists:

$ stty sane

Afterward, inspect the result with stty -a. If the display itself contains stray characters, the shell command reset may also help, but that is a separate terminal utility and is not part of stty.

6. Apply settings to a named device

The default target is the tty connected to standard input. -F selects a device instead:

$ stty -F /dev/pts/3 -a
speed 38400 baud; rows 24; columns 80; line = 0;
...

Replace the path with a device that exists on your host. Reading another terminal may fail because of permissions or because the device is not a tty. Changing it can disrupt another user's session or a running service. Check the owner and your operational change window first; do not use -F as a shortcut around access controls. Elevated privileges may allow the open, but they do not make changing someone else's terminal harmless.

Done means