What is stty ?

posted on
by Mohamad Wael

stty is a tool used to set the input and output settings for the terminal communication interface . It can also read the settings of the terminal communication interface .

In the early days of computing , the terminal was a device used for input and output , and it was connected to a computer. An example of input is the keyboard , and an example of output is the video monitor or the printer.

Printer Terminal
terminal printer 320 by 161
video terminal 320 by 240
Video Terminal

When Unix started , it was written for the PDP computer . The PDP computer had multiple communication interface , such as DL11 the Single Asynchronous Line Interface , and DH11 the 16-Line Programmable Asynchronous interface.

These interface were used to connect terminals , and they were named tty and tty0ttyn by unix.

The tty interface was not programmable , so its transmission speed or its mode could not be set , whereas the tty0ttyn interfaces were programmable , so their transmissions speed and modes could be set.

An example of setting the mode of a programmable interface is : setting it to raw . In raw mode instead of reading a whole line , characters are read one at a time . In addition to that , some special characters such as the # will not work . The # character is used to erase the the last typed character.

Nowadays terminals are emulated in a graphical display . They still act the same way they used to act in unix in accordance with the Single UNIX Specification , IEEE Std 1003.2 (POSIX.2) , which is related to shell and utilities .

The terminal communication interface settings that can be set and read by stty are divided in the following categories :

  • control or special characters : Characters that can control the input , output , and that can send signals for example to interrupt or suspend a program .
  • special settings : For example the speed of the input and output and the number of rows and columns of the terminal .
  • control settings : Related to the terminal hardware properties ; for example , parity bit generation in output and detection in input.
  • input settings : Sets the input modes , for example if input upper case characters are automatically mapped to lower case characters .
  • output settings : Sets the output modes , for example if output lower case characters are automatically mapped to upper case characters .
  • local settings : Related to terminal processing , for example if the terminal will process the delete and signal characters . An example of delete character , is werase which erases the word previous to the currently selected character. An example of signal character is susp which will send a suspend signal to a program .
  • combination settings : Settings that combine one or more settings , for example the oddp setting which combines the parenb , parodd and cs7 settings .
Reading and setting setty modes

Reading the setting of the terminal communication interface

The -a option can be used to read the setting of the terminal communication interface .

			
@debian:~$ stty -a

speed 38400 baud; rows 24; columns 80; line = 0;
# special settings

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;
# special or control characters 


-parenb -parodd -cmspar cs8 -hupcl -cstopb cread -clocal -crtscts
# control settings

-ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon -ixoff
-iuclc -ixany -imaxbel iutf8
# input settings

opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
# output settings 


isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt
echoctl echoke -flusho -extproc
# local settings
					

The control or special characters

The control characters can be used to control the input output and to send signals to programs . stty can set the following control or special characters :

eof

The end of file character is used to terminate the input . By default the end of file character is ctrl-D . ctrl-D is case insensitive .

The wc program with the -lwm option is used to count the number of new lines , words and characters in a file . If no file name is specified or the - option is used, wc can count the number of new lines words and characters from the standard input. ctrl-D can be used to terminate the input.

	
@debian:~$ wc -lwm -
# wc program with :
#	l option to count the number of new lines .
#	w option to count the number of words .
#	m option to count the number of characters .
#	- option to read from standard input .
hello world
hello world
# Written on standard input , hello world 
# followed by an enter , followed by a hello world 
# followed by an enter , and a ctrl-d to terminate
# the input.
      2       4      24 -
# The number of new lines is 2 , the number
# of words is 4 and the number of characters
# is 24 . 
						

The eof character can also be used to terminate the input for the terminal. If ctrl-d is pressed and no other program is running the terminal will exit .

enlighterstty eof <character> can be used to set the terminal eof character . enlighter<character> is to be substituted with an appropriate character for example a , b or a control sequence for example ^n

	
@debian:~$ stty -a
# Display the terminal communication 
# interface settings.
..
eof = ^D; 
...
# eof character is ^D

@debian:~$ stty eof ^n
# set the eof character to ctrl-N
# N is case insensitive. 

@debian:~$ stty -a
# Display the terminal communication 
# interface settings.
..
eof = ^N; 
...
# The eof character is now ctrl-N

						
erase , werase , kill

The erase control character will erase the character previous to the selected one , werase will erase the word previous to the selected character, and kill will erase the current line .

By default erase is assigned the control character ^? , werase is assigned the control character ^W and kill is assigned the control character ^U . The control characters are case insensitive.

enlighterstty erase <character> , enlighterstty werase <character> , and enlighterstty kill <character> can be used to set the terminal erase , werase and kill characters . enlighter<character> is to be substituted with an appropriate character for example a , b or a control sequence for example ^n

	
@debian:~$ stty -a
# Display the terminal communication 
# interface settings.
..
erase = ^?; kill = ^U; werase = ^W; 

...
# erase character is ^?
# werase character is ^W
# kill character is ^U

@debian:~$ stty erase ^e
# set the erase character to ctrl-E
# E is case insensitive . 

@debian:~$ stty werase ^w
# set the werase character to ctrl-W
# W is case insensitive . 

@debian:~$ stty kill ^k
# set the kill character to ctrl-K
# K is case insensitive . 

@debian:~$ stty -a
# Display the terminal communication 
# interface settings.
..
erase = ^E; kill = ^K; werase = ^W; 
...
# erase character is now ^E
# werase character is now ^W
# kill character is now ^K

							
stop , start

The stop control character will stop the output , start will resume it after being stopped .

By default stop is assigned the control character ^S , and start is assigned the control character ^Q . Both control characters are case insensitive .

enlighterstty stop <character> and enlighterstty start <character> can be used to set the terminal stop and start characters. enlighter<character> is to be substituted with an appropriate character for example a , b or a control sequence for example ^n

	
@debian:~$ stty -a
# Display the terminal communication 
# interface settings.
..
start = ^Q; stop = ^S;
...
# start character is ^Q
# stop character is ^S

@debian:~$ stty stop ^s
# set the stop character to ctrl-S
# S is case insensitive .

@debian:~$ stty start ^t
# set the start character to ctrl-T
# T is case insensitive . 

@debian:~$ stty -a
# Display the terminal communication
# interface settings.
..
start = ^T; stop = ^S;
...
# start character is now ^T
# stop character is now ^S

# For example , if the output of a 
# program  is too long , it can be 
# paused and resumed  by using the 
# start and stop control characters .

							
intr

The intr control character sends the interrupt signal , SIGINT to a process . A process should terminate when receiving this signal .

By default intr is assigned the control character ^C . The control character ^C is case insensitive .

enlighterstty intr <character> can be used to set the terminal intr character. enlighter<character> is to be substituted with an appropriate character for example a , b or a control sequence for example ^n

	
@debian:~$ stty -a
# Display the terminal communication 
# interface settings.
..
intr = ^C; 
...
# interrupt character is ^C

@debian:~$ stty intr ^i
# set the intr character to ctrl-I
# I is case insensitive

@debian:~$ stty -a
# Display the terminal communication 
# interface settings.
..
intr = ^I;
...
# interrupt character is now ^I


@debian:~$ ping yahoo.com
PING yahoo.com (72.30.35.10) 56(84) bytes of data.
64 bytes from yahoo.com (72.30.35.10): icmp_seq=1 ttl=63 time=193 ms
64 bytes from yahoo.com (72.30.35.10): icmp_seq=2 ttl=63 time=252 ms
^I
--- yahoo.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 9ms
rtt min/avg/max/mdev = 192.675/222.247/251.820/29.576 ms
# ctrl-I is pressed , this causes 
# a keyboard interrupt signal to be
# sent to ping , ping terminates. 

							
quit

The quit control character sends the quit signal , SIGQUIT to a process . A process should terminate when receiving this signal and create a core dump.

By default quit is assigned the control character ^\ .

enlighterstty quit <character> can be used to set the terminal quit character. enlighter<character> is to be substituted with an appropriate character for example a , b or a control sequence for example ^n

	
@debian:~$ stty -a
# Display the terminal communication 
# interface settings .
..
quit = ^\; 
...
# quit character is ^\

@debian:~$ stty quit ^q
# set the quit character to ctrl-Q
# Q is case insensitive .

@debian:~$ stty -a
# Display the terminal communication 
# interface settings.
..
quit = ^Q;
...
# quit character is now ^Q


@debian:~$ ping yahoo.com
PING yahoo.com (72.30.35.9) 56(84) bytes of data.
64 bytes from media-router-fp1.prod1.media.vip.bf1.yahoo.com (72.30.35.9): icmp_seq=1 ttl=63 time=163 ms
...
# ctrl-Q is pressed , it is ignored by 
# the ping program , and the ping 
# program does not terminate . 

							
susp

The susp control character sends the suspend signal , SIGSTOP to a process . A process is suspended when it receives this signal.

By default susp is assigned the control character ^Z . The control character ^Z is case insensitive .

enlighterstty susp <character> can be used to set the terminal susp character. enlighter<character> is to be substituted with an appropriate character for example a , b or a control sequence for example ^n

	
@debian:~$ stty -a
# Display the terminal communication
# interface settings.
..
susp = ^Z;
...
# susp character is ^Z

@debian:~$ stty susp ^Z
# set the susp character to ctrl-Z
# Z is case insensitive

@debian:~$ stty -a
# Display the terminal communication 
# interface settings.
..
susp = ^Z;
...
# susp character is now ^Z


@debian:~$ ping yahoo.com
PING yahoo.com (72.30.35.9) 56(84) bytes of data.
64 bytes from media-router-fp1.prod1.media.vip.bf1.yahoo.com (72.30.35.9): icmp_seq=1 ttl=63 time=196 ms
64 bytes from media-router-fp1.prod1.media.vip.bf1.yahoo.com (72.30.35.9): icmp_seq=2 ttl=63 time=189 ms
64 bytes from media-router-fp1.prod1.media.vip.bf1.yahoo.com (72.30.35.9): icmp_seq=3 ttl=63 time=187 ms
^Z
[1]+  Stopped                 ping yahoo.com
# ctrl-Z is pressed , this causes the 
# ping program to be suspended.

@debian:~$ fg
# fg causes ping to resume in the foreground. 
ping yahoo.com
64 bytes from media-router-fp1.prod1.media.vip.bf1.yahoo.com (72.30.35.9): icmp_seq=4 ttl=63 time=186 ms
64 bytes from media-router-fp1.prod1.media.vip.bf1.yahoo.com (72.30.35.9): icmp_seq=5 ttl=63 time=183 ms
^C
--- yahoo.com ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 631ms
rtt min/avg/max/mdev = 182.678/188.371/196.300/4.493 ms
# ctrl-C is pressed in line 37, 
# this causes the keyboard inrerrupt signal 
# to be sent to ping , ping terminates . 



							

The special settings

The special settings that can be set by stty are :

speed

enlighterstty <speed> can be used to set the input and output speed in baud. Baud is the number of symbol per seconds . enlighter<speed> is to be substituted by an appropriate speed .

	
@debian:~$ stty -a
# Display the terminal communication 
# interface settings.
speed 38400 baud; 
...
# Terminal speed is 38400 baud

@debian:~$ stty 50
# set the input and output speed to 50 baud

@debian:~$ stty -a
# Display the terminal communication 
# interface settings.
speed 50 baud; 
...
# Terminal speed is 50 baud
							
rows and columns

enlighterstty rows <num_of_rows> and enlighterstty columns <num_of_columns> can be used to set the terminal's number of rows and columns . enlighter<num_of_rows> and enlighter<num_of_columns> are to be substituted by an appropriate number .

	
@debian:~$ stty -a
# Display the terminal communication 
# interface settings.
...
rows 24; columns 80; 
...
# Number of rows is 24 
# number of columns is 80

@debian:~$ stty columns 60
# set the number of columns to 60
@debian:~$ stty rows 30
# set the number of rows to 30

@debian:~$ stty -a
# Display the terminal communication 
# interface settings.
...
rows 30; columns 60; 
...
# Number of rows is 30 
# number of columns is 60
							

The control settings

Control settings are used to control the hardware properties . For example :

parenb

enlighterstty [-] parenb is used to generate the parity bit in the output , and detect the parity bit in the input . A parity bit is used for error detection ; It is a bit which is concatenated to ensure that the number of one's bits is either even or odd .

enlighter[-] is optional , if enlighter- is used the parity bit is disabled , If not it is enabled.

	
@debian:~$ stty -a
# Display the terminal communication 
# interface settings.
...
-parenb 
...
# parity bit generation and detection 
# is disabled .

@debian:~$ stty parenb 
# Enables parity bit generation and detection .

@debian:~$ stty -a
# Display the terminal communication 
# interface settings.
parenb
...
# parity bit generation and detection 
# is enabled .
							

The input settings

Input settings are used to control the input modes . For example :

iuclc

enlighterstty [-] iuclc maps input upper case characters to lower case .

enlighter[-] is optional , if enlighter- is used mapping is disabled , else and if not used the input upper case characters are automatically translated to lower case .

	
@debian:~$ stty -a
# Display the terminal communication 
# interface settings.
...
-iuclc 
...
# Mapping of input upper case , lower case
# is disabled .

@debian:~$ stty iuclc 
# Enables mapping of input upper case
# to lower case . 

@debian:~$ stty -a
# Display the terminal communication 
# interface settings.
iuclc
...
# Mapping of input upper case to lower
# case is enabled . Any input
# upper case letter is automatically
# translated to lower . 
							

The output settings

Output settings are used to control the output modes . For example :

olcuc

enlighterstty [-] olcuc maps output lower case characters to upper case .

enlighter[-] is optional , if enlighter- is used mapping is disabled , else and if not used output lower case characters are automatically translated to upper case .

	
@debian:~$ stty -a
# Display the terminal communication 
# interface settings.
...
-olcuc 
...
# Mapping of output lower case , upper case
# is disabled .

@debian:~$ stty olcuc 
# Enable mapping of output lower case
# to upper case . 

@debian:~$ STTY -A
# Display the terminal communication 
# interface settings. Output
# is automatically mapped to upper
# case , so stty -a is displayed 
# as STTY -A 
OLCUC
...
# Mapping of output lower case to upper
# case is enabled . Any output
# lower case letter is automatically
# translated to upper . 
							

The local settings

The local settings affect the terminal processing , such as if the terminal will process some special characters like the delete or signal characters . Some settings that could be set are :

echo

enlighterstty [-] echo affects whether or not the terminal will display the input characters .

enlighter[-] is optional , if enlighter- is used the terminal will not display the input characters , else and if enlighter- is not used the terminal will display the input characters .

	
@debian:~$ stty -a
# Display the terminal communication 
# interface settings.
...
echo
...
# echo is on , the terminal will display 
# the input characters .

@debian:~$ stty -echo
# Set the terminal to not display
# the input characters .

@debian:~$ -echo
# stty -a is typed but it is not 
# displayed by the terminal . The 
# result is :
# ...
# -echo
# ....
# The terminal is set to not 
# display the input characters . 
							
isig

enlighterstty [-] isig affects whether or not the control characters INTR , QUIT and SUSP used to send signals to programs are processed or not .

enlighter[-] is optional , if enlighter- is used the INTR , QUIT and SUSP control characters are ignored , else they are processed.

	
@debian:~$ stty -a
# Display the terminal communication 
# interface settings.
intr = ^C; quit = ^\; susp = ^Z; 
# The interrupt special character is 
# ctrl-C , the quit special character
# is ctrl-\ , the suspend special 
# character is ctrl-Z . 
...
isig
...
# isig is on . The special 
# signal characters are processed .

@debian:~$ stty -isig
# Disable the control characters
# intr , quit , susp

@debian:~$ stty -a
# Display the terminal communication 
# interface settings.
-isig
# The control characters intr , quit , susp
# are disabled , and will not work 

@debian:~$ ping yahoo.com
PING yahoo.com (72.30.35.9) 56(84) bytes of data.
64 bytes from yahoo.com (72.30.35.9): icmp_seq=1 ttl=63 time=182 ms
^C64 bytes from yahoo.com (72.30.35.9): icmp_seq=2 ttl=63 time=185 ms
64 bytes from yahoo.com (72.30.35.9): icmp_seq=3 ttl=63 time=465 ms
...
# The  interupt signal ctrl-c is sent in 
# line 29 but it is ignored .


							

The combination settings

These are settings that combine or more settings . For example :

oddp

enlighterstty oddp combines the parenb , parodd and cs7 settings . parenb enables parity bit generation in output , and detection in input . parodd sets the parity bit to odd . cs7 sets the character size to 7 bits.

enlighterstty - oddp combines the -parenb and cs8 settings . - parenb disables parity bit generation in output , and detection in input . cs8 sets the character size to 8 bits.