Linux stty Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the stty command in Linux to configure and control the settings of a terminal or serial port. The lab covers the purpose and syntax of the stty command, as well as practical examples of modifying terminal settings such as baud rate, number of rows and columns, and various control characters. This lab is part of the System Configuration and Settings skill set, and the stty command is a widely used tool for managing terminal settings in Linux.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux/BasicSystemCommandsGroup -.-> linux/sleep("`Execution Delaying`") subgraph Lab Skills linux/sleep -.-> lab-422935{{"`Linux stty Command with Practical Examples`"}} end

Understand the Purpose and Syntax of the stty Command

In this step, you will learn about the purpose and syntax of the stty command in Linux. The stty command is used to configure and control the settings of a terminal or serial port.

First, let's understand the basic syntax of the stty command:

stty [OPTION] [SETTING]

The OPTION can be one of the following:

  • -a: Display all current settings
  • -g: Print all current settings in a form that can be used as an argument to another stty command

The SETTING can be one of the many terminal settings, such as:

  • speed: Set the terminal baud rate
  • rows: Set the number of rows
  • cols: Set the number of columns
  • intr: Set the interrupt character
  • quit: Set the quit character
  • erase: Set the erase character
  • kill: Set the kill character
  • eof: Set the end-of-file character

Let's try some examples to understand the stty command better:

## Display all current terminal settings
stty -a

Example output:

speed 38400 baud; rows 24; 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; discard = ^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 -flusho -extproc

This output shows the current terminal settings, including the baud rate, number of rows and columns, and various control characters.

Now, let's try to change some of the settings:

## Set the terminal baud rate to 9600
stty speed 9600

## Set the number of rows to 30
stty rows 30

## Set the number of columns to 100
stty cols 100

After running these commands, you can verify the changes by running stty -a again.

Modify Terminal Settings Using the stty Command

In this step, you will learn how to modify various terminal settings using the stty command.

First, let's check the current terminal settings:

stty -a

This will display all the current terminal settings, including the baud rate, number of rows and columns, and various control characters.

Now, let's try to modify some of these settings:

## Set the terminal to use 7-bit character mode
stty cs7

## Set the terminal to use 8-bit character mode
stty cs8

## Set the terminal to use no parity
stty -parenb

## Set the terminal to use even parity
stty parenb -parodd

## Set the terminal to use odd parity
stty parenb parodd

## Set the terminal to use hardware flow control
stty crtscts

## Set the terminal to use software flow control
stty -crtscts ixon ixoff

## Set the terminal to ignore break conditions
stty -ignbrk

## Set the terminal to generate a signal when a break condition is detected
stty ignbrk brkint

After running these commands, you can verify the changes by running stty -a again.

Practical Examples of Using the stty Command

In this final step, you will explore some practical examples of using the stty command.

  1. Disabling the Ctrl+C Interrupt
## Disable Ctrl+C interrupt
stty -intr

This will disable the Ctrl+C interrupt, which is typically used to terminate a running process. You can verify this by running a command that would normally be interrupted by Ctrl+C, such as sleep 60.

  1. Changing the Erase Character
## Change the erase character to Backspace
stty erase ^?

This will change the erase character from the default (usually Delete) to Backspace. You can test this by typing some text in the terminal and pressing Backspace.

  1. Changing the Terminal Size
## Set the terminal size to 80 columns and 24 rows
stty cols 80 rows 24

This will change the terminal size to 80 columns and 24 rows. You can verify the change by running stty -a or using the resize command.

  1. Saving and Restoring Terminal Settings
## Save the current terminal settings
stty -g > terminal_settings.txt

## Restore the saved terminal settings
stty $(cat terminal_settings.txt)

This example shows how to save the current terminal settings to a file and then restore them later. This can be useful if you need to temporarily change some settings and then revert back to the original configuration.

Summary

In this lab, you learned about the purpose and syntax of the stty command in Linux, which is used to configure and control the settings of a terminal or serial port. You explored how to display the current terminal settings using the -a option, as well as how to modify various settings such as baud rate, number of rows and columns, and control characters. The lab provided practical examples to help you understand the usage of the stty command and its versatility in managing terminal configurations.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like