LinuxCommandLibrary

stty

Set or print terminal I/O characteristics

TLDR

Display current terminal size

$ stty size
copy

Display all settings for the current terminal
$ stty [[-a|--all]]
copy

Set the number of rows or columns
$ stty [rows|cols] [count]
copy

Get the actual transfer speed of a device
$ stty [[-F|--file]] [path/to/device_file] speed
copy

Reset all modes to reasonable values for the current terminal
$ stty sane
copy

Switch between raw and normal mode
$ stty [raw|cooked]
copy

Turn character echoing off or on
$ stty [-echo|echo]
copy

Display help
$ stty --help
copy

SYNOPSIS

stty [OPTION]... [SETTING]...

Example usage:
stty -a (show all settings)
stty raw -echo (set raw mode, disable echo)
stty 9600 -F /dev/ttyS0 (set baud rate for a serial port)

PARAMETERS

-a, --all
    Displays all current terminal settings in a human-readable format.

-g, --save
    Prints the current terminal settings in a format that can be used as an argument to another stty command, useful for saving and restoring configurations.

-F DEVICE, --file=DEVICE
    Applies or queries settings for the specified terminal DEVICE instead of the standard input.

raw
    Sets the terminal to raw mode: input is passed character-by-character without processing, echoing, or special character interpretation.

-raw
    Disables raw mode, typically returning to canonical mode.

cooked
    Sets the terminal to canonical (cooked) mode: input is line-buffered, and special characters (e.g., backspace, Ctrl+C) are interpreted. This is the default.

sane
    Resets all terminal settings to a 'sensible' or default state, often used to recover from misconfigured terminals.

echo
    Enables echoing of input characters to the terminal.

-echo
    Disables echoing of input characters (e.g., for password entry).

icanon
    Enables canonical input processing (line buffering).

-icanon
    Disables canonical input processing (enables raw mode-like behavior for input).

isig
    Enables interpretation of special control characters like INTR (Ctrl+C), QUIT (Ctrl+\), and SUSP (Ctrl+Z).

-isig
    Disables interpretation of signal-generating control characters.

ixon
    Enables XON/XOFF flow control on input.

-ixon
    Disables XON/XOFF flow control.

opost
    Enables output post-processing, such as mapping newlines to carriage return-newline sequences.

-opost
    Disables output post-processing.

[SPEED]
    Sets the input and output baud rate (e.g., 9600, 115200).

eof CHAR
    Sets the End-Of-File character (default Ctrl+D).

intr CHAR
    Sets the Interrupt character (default Ctrl+C).

DESCRIPTION

stty is a fundamental Linux command used to configure and display the settings of a terminal interface. These settings determine how the operating system interacts with the terminal device, which can be a physical serial port, a virtual console, or a pseudo-terminal (like those used by SSH sessions or graphical terminal emulators).

The command allows users to examine and modify various input, output, and control parameters that influence character processing, flow control, character echoing, line buffering, and special character definitions (like interrupt, erase, or end-of-file). For instance, it can switch between canonical (line-buffered) and non-canonical (raw) input modes, enable or disable character echoing, or set the baud rate for serial communication.

stty interacts directly with the kernel's termios interface, providing granular control over the terminal's behavior. It is invaluable for troubleshooting terminal-related issues, customizing shell environments, or setting up serial communications.

CAVEATS

stty directly manipulates kernel terminal driver settings, which can have significant consequences if used incorrectly.

Potential for Unusable Terminal: Incorrect settings (e.g., stty raw -echo without a plan to revert) can make your terminal session unresponsive or seemingly "frozen," as input may not be echoed and control characters might not be interpreted. Always be cautious, and consider opening a second terminal session as a fallback.

Session-Specific: Changes made with stty are typically temporary and apply only to the current terminal session. For persistent changes, settings need to be applied in shell initialization files (e.g., .bashrc, .profile) or system-wide configuration.

System Differences: While core functionality is standard, specific options or default behaviors can vary slightly between different Unix-like operating systems (e.g., Linux vs. BSD) or even different terminal emulators.

<B>UNDERSTANDING TERMINAL MODES</B>

Terminal settings managed by stty are typically categorized into:

  • Input Modes (c_iflag): Control how input characters are processed (e.g., parity checking, newline handling, flow control).
  • Output Modes (c_oflag): Govern how output characters are processed before transmission (e.g., character mapping, delays).
  • Control Modes (c_cflag): Define hardware-related settings like baud rate, character size (bits per character), and parity settings.
  • Local Modes (c_lflag): Affect local line discipline, such as echoing, canonical mode, and signal generation.
  • Special Characters (c_cc): Define the characters used for specific functions like interrupt (INTR), erase (ERASE), kill (KILL), and end-of-file (EOF).

<B>SAVING AND RESTORING SETTINGS</B>

A common and powerful use of stty is to save and restore terminal configurations. The command stty -g outputs the current settings in a format that can be directly used as arguments to another stty command.

Example:
OLD_TTY_SETTINGS=$(stty -g)
stty raw -echo # Perform raw operations
stty $OLD_TTY_SETTINGS # Restore original settings

This technique is invaluable for scripts that temporarily alter terminal behavior, ensuring that the user's environment is returned to its original state.

HISTORY

The stty command is a venerable utility, tracing its origins back to the early days of Unix. It was designed to interact with the underlying terminal driver and adjust various input/output processing modes. Its functionality has evolved alongside terminal hardware and operating system interfaces, moving from simpler sgtty structures to the more comprehensive termio and eventually the POSIX-standardized termios interface.

Despite the rise of graphical user interfaces and terminal emulators, stty remains a crucial tool for managing pseudo-terminals, configuring serial communication, and debugging low-level terminal behavior, embodying a core aspect of Unix's interaction with character devices.

SEE ALSO

termios(3), tty(4), ioctl(2), setterm(1), tput(1), screen(1), tmux(1)

Copied to clipboard