How to Use the Command 'stty' (with examples)
The ‘stty’ command is a powerful tool designed for configuring and displaying settings of the terminal interface. It allows users to control various aspects of terminal operation, such as input/output settings, line speed, and control characters. With ‘stty,’ users can tailor the behavior and appearance of their terminal sessions to their specific needs. This command is part of the GNU Core Utilities and is frequently used by system administrators and advanced users to fine-tune their terminal environments.
Use Case 1: Display All Settings for the Current Terminal
Code:
stty --all
Motivation:
The motivation for using stty --all
is to gain visibility into the current configuration of the terminal’s interface. Whether you are troubleshooting a problem, verifying settings before making changes, or learning about how your terminal is configured, this command provides a comprehensive overview of all current settings. With this information, users can understand how input and output are managed, what kind of control characters are in use, and other system-specific configurations that might be affecting terminal performance.
Explanation:
stty
: The command itself, which stands for “set tty.”--all
: This option instructs ‘stty’ to display all of the current settings in a verbose and comprehensive manner, encompassing all the various aspects and parameters of the terminal.
Example Output:
speed 9600 baud; rows 24; columns 80; line = 0;
intr = ^C; quit = ^\; erase = ^H; kill = ^U; eof = ^D;
...
The output provides a detailed list where each parameter’s current value is displayed, including speed, window size, and key bindings for interrupt, erase, and other control functions.
Use Case 2: Set the Number of Rows or Columns
Code:
stty rows 40
Motivation:
Changing the number of rows (or columns) in the terminal can be essential for ensuring that applications display content optimally. Some programs, depending on their output style, might require a specific terminal size to function properly or display results in a readable format. By customizing the number of rows, users can enhance readability, improve terminal layout, and minimize scrolling—especially useful when working with scripts or logs that output large amounts of data.
Explanation:
stty
: The command name.rows
: This option selects the specific parameter to adjust, in this case, the number of text rows in the terminal.40
: The target value you’re setting for the rows. This example sets the terminal to have 40 rows.
Example Output:
There’s no standard output for this command when executed successfully. The row configuration will change silently unless there is an error, such as an invalid number of rows, in which case an error message will be displayed.
Use Case 3: Get the Actual Transfer Speed of a Device
Code:
stty --file /dev/ttyS0 speed
Motivation:
Knowing the data transfer speed for a device is critical for diagnosing communication issues or optimizing serial interface performance. When working with devices connected via serial ports, like modems or embedded systems, checking the configured speed can clarify if the device operates at the expected baud rate. This information is imperative for adjusting configurations or troubleshooting connectivity issues between systems designed to communicate over different speeds.
Explanation:
stty
: The core command.--file /dev/ttyS0
: Specifies the device file associated with the terminal interface you wish to inspect. ‘/dev/ttyS0’ is a common representation for the first serial port on Unix-like systems.speed
: Queries the current speed (baud rate) setting for data transmission on the specified device.
Example Output:
9600
This indicates that the current transfer speed for the device /dev/ttyS0
is set to 9600 baud.
Use Case 4: Reset All Modes to Reasonable Values for the Current Terminal
Code:
stty sane
Motivation:
Sometimes, terminal settings can become garbled or misconfigured due to scripts, accident, or application errors, leading to unusual terminal behavior. This can include issues like unreadable fonts or problems with keyboard input. Using stty sane
is a quick way to revert the terminal to a set of standard, reasonable defaults. This is incredibly useful for recovering from configuration chaos without needing to understand or modify each individual setting manually.
Explanation:
stty
: The command used to manipulate terminal settings.sane
: A special option that restores a standard set of terminal settings, often considered the default or ‘safe’ mode, ensuring that the terminal is left in a stable and predictable state.
Example Output:
As with changing the rows or columns, executing stty sane
won’t produce any visible output if successful. The terminal settings reset quietly unless there is an error during the execution.
Conclusion
The ‘stty’ command is an essential utility for manipulating terminal device parameters, offering granular control over the terminal’s behavior and appearance. Whether you’re setting the terminal size, troubleshooting speed issues, or resetting to a sane state, ‘stty’ provides the tools needed for comprehensive terminal management. Through these examples, users can gain a deeper understanding of how the ‘stty’ command can benefit their daily workflows and terminal usage.