How to use the command "stty" (with examples)
The “stty” command is used to set options for a terminal device interface. It allows users to customize various settings for their terminal. This command is particularly useful for controlling the behavior of the terminal, such as the number of rows or columns, transfer speed, and resetting all modes.
Use case 1: Display all settings for the current terminal
Code:
stty --all
Motivation: The “stty –all” command displays all the settings for the current terminal. This is useful when you want to check the current configurations of your terminal, such as baud rate, line discipline, and terminals settings.
Explanation: “–all” is an option that tells the “stty” command to display all settings for the current terminal. It provides detailed information about the terminal’s configuration.
Example output:
speed 38400 baud; rows 50; columns 80; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>;
eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R;
werase = ^W; lnext = ^V; flush = ^O; min = 1; time = 0;
-parenb -parodd cs8 hupcl -cstopb cread -clocal -crtscts
-ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon -ixoff
-iuclc ixany imaxbel -iutf8
opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
-isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt
-echoctl -echoke -defecho -flusho -extproc
Use case 2: Set the number of rows or columns
Code:
stty rows 24
Motivation: The “stty rows” command is used to set the number of rows in the terminal. Adjusting the number of rows can be useful when working with applications that require a specific terminal size or for improving readability.
Explanation: “rows” is an argument that specifies the number of rows for the terminal. In the example above, we set the number of rows to 24.
Example output: N/A (No output is displayed when setting the number of rows/columns)
Use case 3: Get the actual transfer speed of a device
Code:
stty --file /dev/ttyUSB0 speed
Motivation: The “stty –file” command allows users to check the actual transfer speed of a device. This is particularly useful when troubleshooting communication issues or verifying the correct speed settings.
Explanation: “–file” is an option that specifies the path to the device file. In the example above, we are checking the transfer speed of the “/dev/ttyUSB0” device file.
Example output:
speed 9600 baud; line = 0;
Use case 4: Reset all modes to reasonable values for the current terminal
Code:
stty sane
Motivation: The “stty sane” command is used to reset all modes to reasonable values for the current terminal. This can be useful when you want to revert any changes made to the terminal’s configuration and ensure its settings are at their default state.
Explanation: “sane” is an argument that tells the “stty” command to reset all modes to reasonable values for the current terminal.
Example output: N/A (No output is displayed when resetting all modes)
Conclusion:
The “stty” command provides powerful customization options for the terminal device interface. It allows users to control various settings, such as the number of rows or columns, transfer speed, and resetting all modes. Understanding how to use these different use cases can greatly improve the user experience and help troubleshoot any terminal-related issues.