How to Use the stty Command to Change Terminal Line Settings in Linux

When you type a password in a Linux terminal and the characters do not appear on the screen, or when you press Ctrl+C and the current running process instantly terminates, you are interacting with the terminal line discipline. The terminal line discipline is a software layer between the physical keyboard input and the actual shell program. It dictates how raw keystrokes are processed before they ever reach the application. If you need to remap these special control characters or change how the terminal handles input and output, you must use the stty (set teletype) command.

Viewing Current Terminal Settings

Before making any changes, it is always a good idea to see exactly how your terminal is currently configured. You can print all current terminal line settings to standard output by using the -a (all) flag.

stty -a

The output will display several lines of configuration data. The most important section is usually the list of special control characters. For example, you will see entries like intr = ^C; (Interrupt is mapped to Ctrl+C) and erase = ^?; (Erase/Backspace is mapped to the Delete key character).

Changing Special Control Characters

One of the most common uses for stty is to fix broken backspace or delete keys when connecting to an older UNIX server via SSH. If pressing Backspace prints weird symbols like ^H instead of deleting a character, you can use stty to remap the erase function.

stty erase ^H

This tells the terminal that whenever it receives the ^H control code, it should execute the “erase” action instead of printing a literal symbol.

Disabling Terminal Echo (Hiding Input)

When you write a bash script that prompts a user for a sensitive password, you do not want their keystrokes to be visible on the screen for anyone walking by to see. You can achieve this by temporarily disabling the echo setting.

stty -echo

Notice the minus sign (-) before the word echo. In stty syntax, a minus sign means “turn this feature off.” After running this command, anything you type will be invisible. The shell will still receive the data, but it will not bounce the characters back to the monitor.

To turn the echo feature back on after the user has submitted their password, run the command again without the minus sign:

stty echo

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.