Professional Documents
Culture Documents
A Guide To The Terminal, Console, and Shell
A Guide To The Terminal, Console, and Shell
dev/guide-terminal-shell-console/
Menu
!"Home
!"All Articles
!"Let's Connect
!"Books
!"Support
!"About
Categories
!"Complexity
!"Fundamentals
!"Mouseless
!"Vim
!"Soft Skills
!"Learning
!"Side Projects
RSS
!"From the Telegraph to the Video Terminal
☰ MENU
#"The Telegraph
#"The Teleprinter, Teletype, or TTY
#"Computers and TTYs
#"Video Terminals
1 of 41
!"Unix System in AT&T 12/28/22, 9:28 AM
A Guide to the Terminal, Console, and Shell https://thevaluable.dev/guide-terminal-shell-console/
The
Let's
Valuable Connect
Books Support About
Dev
☰ MENU
3 of 41 12/28/22, 9:28 AM
A Guide to the Terminal, Console, and Shell https://thevaluable.dev/guide-terminal-shell-console/
“ Once upon a time, as far as I can tell, There was the console,
the terminal, and the shell. Let me tell you the story of these di- ☰ MENU
Why does Davina want her colleagues to know more about the vir-
tual consoles, the terminal, and the shell?
I don’t know any developer who doesn’t use a terminal, a shell, and
some CLIs. I definitely use them all the time. They are the central
building bricks of my Mouseless Development Environment.
So, since it’s so useful, let’s look a bit deeper what’s this shell, con-
sole, and terminal. More precisely, we’ll see, in this article:
This article is the result of a reader asking me to write about the ter-
minal. Don’t hesitate to subscribe to the newsletter to ask me what
you’d like to read in The Valuable Dev!
Strap on in the time machine, this will be a wild ride. The terminal
carry a lot of baggage from devices of forgotten eras, so let’s come
back in time!
The Telegraph
The history of the telegraph goes back from the 17th and 18th cen-
tury. At that time, there were already some ways to send telegraphs
on long distances! The technology jumped forward in the 18th cen-
tury: during the french revolution, there was suddenly an urgent
need for the telegraph; after all, the French monarchy (and the
king’s head) was on the line. That’s why the telegraph expanded
quickly in France first, and then in Europe.
To send a telegraph, you needed two persons on each side of the line
(emission and reception): the operators. At the time, it was not pos-
5 of 41 12/28/22, 9:28 AM
A Guide to the Terminal, Console, and Shell https://thevaluable.dev/guide-terminal-shell-console/
Let’s say that you’re a respectable citizen from Berlin in the 19th
century, and you want to send a telegraph to your best friend in
Paris. You would need to go to the telegraph office in Berlin, and
give your message (in plain text) to the operator there. He would en-
crypt it, and send it to Paris. There, a second operator would decode
your message, write it somewhere, and give it to Dave, your best
friend.
Berlin Paris
For example, there were also the optical telegram, the helograph
telegram, even wireless telegraphy was a thing.
6 of 41 12/28/22, 9:28 AM
A Guide to the Terminal, Console, and Shell https://thevaluable.dev/guide-terminal-shell-console/
☰ MENU
Berlin Paris
With the adoption of the teleprinter, the Morse system wasn’t the
best code to send messages anymore; it wasn’t really machine
friendly. That’s where the Baudot code entered the chat: each char-
acter had the same length, making it easier for the teleprinters to
handle. It could also encrypt more characters! Five bit sequential of
binary code, allowing 32 (2^5) possible characters in your mes-
sages. Even better: a “shift” key (or FIGS, for “figure shift”) was al-
lowing numbers and special characters in the messages for the first
7 of 41 12/28/22, 9:28 AM
A Guide to the Terminal, Console, and Shell https://thevaluable.dev/guide-terminal-shell-console/
time.
☰ MENU
The Baudot code had also some early control characters (CR). For
example, let’s say that you wanted to send a message, but you made
a typo: you could use the “DEL” code to say that you wanted the
character before it to be deleted. When the message was received,
the teleprinter in charge of printing back the message wouldn’t print
the “DEL” code, but instead it would skip the character just before
it. That’s also why control characters are also called non-printable
characters.
In 1925, the Baudot code was improved, and became the Murray
code. More control characters were included: the carriage return for
example (CR), to move the carriage (the “cursor” of the teleprinter)
back to the left margin of the same line. You also had the line feed
(LF), to advance the carriage to the same column of the next line.
These control characters are still used today. We’ll come back to
them below in this article.
And guess what: you can still display the speed in baud of your ter-
minal. It doesn’t mean anything anymore, but it shows the roots of
the terminal coming back to the teleprinter.
8 of 41 12/28/22, 9:28 AM
A Guide to the Terminal, Console, and Shell https://thevaluable.dev/guide-terminal-shell-console/
send bigger messages than with the Baudot (or the Murray) code,
even including the luxe of lowercase and uppercase letters.
It was a great step toward standardisation, too. Until then, many dif-
ferent codes to encrypt messages were still used, even if some more
than others. With the ASCII code, teleprinters in the US were using
the same set of characters to send messages.
Last thing: the teleprinter is the official name for the device.
Teletype was a major brand producing teleprinters, that’s why “tele-
type” became a synonym of “teleprinter”. The abbreviation of
t ele ty pe is TTY.
to a computer.
☰ MENU
10 of 41 12/28/22, 9:28 AM
A Guide to the Terminal, Console, and Shell https://thevaluable.dev/guide-terminal-shell-console/
☰ MENU
Two wires were necessary: one to send the data to the computer, an-
other one to receive it. ☰ MENU
An ASR-33 - source
control sequences are also called escape sequences. This prefix was
not a standard however, but widely used nonetheless. ☰ MENU
Video Terminals
In 1960, IBM began to experiment with a new way to interact with
computers, the “glass teletype”, a terminal with screen. The video
terminal was born.
This video terminal was still an external device to the computer it-
self, including both a screen and a keyboard. Like most invention, it
was very expensive at the beginning, limiting its adoption. The
price dropped in the middle of the 70s, and the success of Video ter-
minal rose.
13 of 41 12/28/22, 9:28 AM
A Guide to the Terminal, Console, and Shell https://thevaluable.dev/guide-terminal-shell-console/
☰ MENU
The DEC VT100 (for V ideo T erminal) was one of the most popular
choice. Released in 1978, it was one of the first video terminal sup-
porting a new set of escape sequences, the ANSI escape codes.
When connected to a computer, this terminal was called a “control-
ling terminal”, because it was controlling the computer.
In 1970, Dennis Ritchie and Ken Thompson develop the UNIX op-
erating system on a DEC PDP-11, a popular minicomputer series☰ MENU
at
the time. It was an interactive computer, easier to program than the
competitors. It influenced not only modern CPUs (like the Intel x87)
but also other operating systems, like the CP/M, the ancestor of MS-
DOS (the base of Windows for many years).
Unix was created for AT&T needs, and their creators were influ-
enced by the long time they spent on teletypes.
device with a file prefixed by “tty”. This file was an interface be-
tween the external terminal and the computer. ☰ MENU
The Shell
When you think about it, teleprinters and video terminals were quite
dumb:
But what program are we talking about, here? What can interpret
our commands? That’s where the shell shines.
Types of Terminal
Emulators
As time went on, with the development of OS (Operating Systems)
like Unix and CP/M, the video terminals began to be abstracted
away.
With computers getting smaller and smaller, more and more “per-
sonal” (the first widely adopted microcomputer was the Apple II in
17 of 41
1977), why needing a physical external device when you can emu- 12/28/22, 9:28 AM
A Guide to the Terminal, Console, and Shell https://thevaluable.dev/guide-terminal-shell-console/
Imagine that you just threw away your big video terminal. You only
have a screen and a keyboard attached to a computer now, and this
time the computer take your input from your keyboard, and display
the output on the screen. No need of a video terminal anymore!
Now, when you start your computer, you’re first greeted with a lo-
gin screen. You log in with your favorite user, and then the shell
will display a prompt, inviting you to enter some commands. You
can type these commands in your virtual console and press ENTER ,
to send them to the shell, where they will be interpreted. The output
18 of 41 12/28/22, 9:28 AM
A Guide to the Terminal, Console, and Shell https://thevaluable.dev/guide-terminal-shell-console/
appears on your screen. You can even start other processes, like
your favorite editor, which is obviously Vim. ☰ MENU
The virtual console itself runs in the kernel, and your shell as well
as its child processes run in user land. Wait, what?
If the process needs to ask the kernel to use some hardware (like
19 of 41 12/28/22, 9:28 AM
A Guide to the Terminal, Console, and Shell https://thevaluable.dev/guide-terminal-shell-console/
writing on the hard disk, or using the RAM, for example), it will
send a system call to the kernel. The kernel will receive the system
☰ MENU
call, enable some more protections (on the CPU for example), and it
will try to handle what the process wants.
As a result, not many OSes give us the chance to interact with vir-
tual consoles nowadays; but Linux-based systems still have them.
The Linux kernel was developed with multi-users in mind from the
beginning, giving us multiple virtual consoles where different users
could log in in parallel.
When you launch some Linux distribution (like Arch Linux, by the
way) without any desktop installed, you’ll be greeted with a logging
screen.
mands.
☰ MENU
If you switch to the second virtual console, you’ll see your input
displayed on the screen.
If you try to read the files themselves (using cat for example), you
21 of 41
won’t see anything in there; they’re just used as interface to pass 12/28/22, 9:28 AM
A Guide to the Terminal, Console, and Shell https://thevaluable.dev/guide-terminal-shell-console/
You can also use the CLI chvt to switch to different virtual con-
soles: chvt 2 will switch to the second virtual console for example.
22 of 41 12/28/22, 9:28 AM
A Guide to the Terminal, Console, and Shell https://thevaluable.dev/guide-terminal-shell-console/
Pseudoterminals, or PTY
A virtual console is running in the kernel. A pseudoterminal (or
“PTY”) is a terminal running in user land. Any terminal you launch
from a graphical interface (like a desktop) is a PTY.
Let’s imagine that you open three terminal emulators in your fa-
vorite graphical environment, like xterm for example. On Linux-
based systems, it will first open the file /dev/ptmx , which will then
open two other files:
Then, a shell will be attached to the PTS, receiving its input from
the PTS file. When you’re trying to run some commands in your
pseudoterminal, the input will first flow from the PTM to the PTS,
and then from the PTS to the shell. The shell’s output will take the
same path back.
24 of 41 12/28/22, 9:28 AM
A Guide to the Terminal, Console, and Shell https://thevaluable.dev/guide-terminal-shell-console/
User land
Kernel
PTS (/dev/pt2)
shell
process 1 process 2
Coming back to physical teletypes, we’ve seen above that they were
connected by a pair of wires to a computer. You can see the master
25 of 41 12/28/22, 9:28 AM
A Guide to the Terminal, Console, and Shell https://thevaluable.dev/guide-terminal-shell-console/
in user land to the kernel. The slave file “PTS” has the same role as
the TTY ( /dev/tty<tty_number> ) file of a virtual console: it’s an
interface between the terminal and the different processes using our
commands, like the shell.
Again, you can use the CLI tty in your terminal emulator to see
what PTS file you’re writing to. You can also try to write to another
PTS (for example echo "hello third PTY" > /dev/pts/3 ), you’ll
see in the third pseudoterminal what you’ve written.
We’ve represented the TTY device as a black box until now, but
there’s more to it. We can divide it in three parts: the TTY core, the
line discipline, and the TTY drivers.
Thanks to the TTY core, you can write some commands in your ter-
minal. These commands are then send to the line discipline, which
can also intercept control characters and escape sequences. For ex-
ample, as we already observed, the control sequence “^[[3~” will be
send to the line discipline if I hit the key “DEL” in my terminal; as a
result, the line discipline will get back your input with a character
deleted. This control sequence is also called escape sequence, be-
cause the character ^[ represents the ESC key.
We were saying above that a terminal is quite dumb. The line disci-
pline makes it a bit smarter, because it already interprets some of
your input. It’s a very basic editor, if you will.
With early computers, you didn’t have much choice: you had to use
the line discipline. Its buffer was useful to store characters, instead
of using the very limited RAM. But nowadays, with our crazy com-
puters, it’s not a problem anymore.
TTY Drivers
When the line discipline is done processing your input, your com-
mand (or individual characters, depending on the mode you’re in) is
then sent to the TTY drivers. They will interact with the hardware
directly (the role of a driver), and also pass the different characters
to the processes, like the shell.
The possible output goes through the TTY drivers again, then back
to the line discipline, which might convert some other control se-
quences. For example, on Unix systems, it converts the line feeds
(LF) from the output to the combo carriage return/line feed
(CR/LF), simply because the line feed only advance the carriage re-
turn to the same column on the next line, not on the first column on
the next line.
We’ll see how to change the behavior of the line discipline in the
next section. For now, here’s a diagram for a whole TTY device:
28 of 41 12/28/22, 9:28 AM
A Guide to the Terminal, Console, and Shell https://thevaluable.dev/guide-terminal-shell-console/
TTY Device
☰ MENU
hardware
shell
It’s more or less the same for a pseudoterminal, except that the line
discipline sits on top of the PTS.
stty -a
It will output the different settings you can configure for the line
discipline of any terminal emulator (pseudoterminal or virtual con-
sole).
29 of 41 12/28/22, 9:28 AM
A Guide to the Terminal, Console, and Shell https://thevaluable.dev/guide-terminal-shell-console/
The speed is another artifact from the bygone era when physical
teletypes were ruling the computer world. The rows, columns, and
line of the terminal are not always accurate either.
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol
= <undef>; eol2 = <undef>; swtch = <undef>;
start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W;
30 of 41 12/28/22, 9:28 AM
A Guide to the Terminal, Console, and Shell https://thevaluable.dev/guide-terminal-shell-console/
stty intr ^R
You can use here the hat notation for representing CTRL+r : a carret
^ represents here the CTRL key. You can also type the actual control
character ^R by first hitting CTRL+v , followed by CTRL+r .
You can also run stty --help to learn more about all these bind-
ings. Here are the most interesting to me:
31 of 41 12/28/22, 9:28 AM
A Guide to the Terminal, Console, and Shell https://thevaluable.dev/guide-terminal-shell-console/
You can also use undef to disable a control character. For example:
-extproc
☰ MENU
The name of these settings are quite obscure. The first letter can
help guess what they stand for: when the settings are about the out-
put of the TTY, they will begin with an o . Same for input: they will
begin with an i .
This will disable the setting opost , and enable the setting tostop .
fect, by hitting
CTRL+j .
stty igncr
Now, you can’t send any command to your shell anymore… but you
34 of 41
can still use CTRL+j to send a line feed control character, which is 12/28/22, 9:28 AM
A Guide to the Terminal, Console, and Shell https://thevaluable.dev/guide-terminal-shell-console/
Setting Equivalent
It’s because the line editor of your shell might reset some TTY op-
tions automatically, because it can’t work with (or without) these
options.
35 of 41 12/28/22, 9:28 AM
A Guide to the Terminal, Console, and Shell https://thevaluable.dev/guide-terminal-shell-console/
stty -echo
stty -a | grep "echo"
The output will show me that the option echo is still enabled. It was
disabled when I sent the first command to the shell, but then ZLE
(Zsh’s line editor) enabled it again.
For the first, you can run for example the following:
The option echo will be disabled for the new cat process (which
means that you’ll only see the output of cat , not the input you’re
giving to the terminal), but then when cat ends, ZLE will automati-
cally enable echo back.
36 of 41 12/28/22, 9:28 AM
A Guide to the Terminal, Console, and Shell https://thevaluable.dev/guide-terminal-shell-console/
If you really want to only use the terminal editing power without
any line editing, you can also turn down readline (for Bash) or ZLE
☰ MENU
bash --noediting
unsetopt zle
The first command will create a new Bash process with readline
(Bash’s line editor) disabled, the second one will only switch off
ZLE. To switch it back on, you can run setopt zle .
Last thing: we can also fetch the terminal settings for another TTY.
For example, on Linux:
stty -a -F /dev/pts/1
A Selection of Terminal
Emulators
It wouldn’t be fair to speak about terminal emulators without rec-
ommending some. Here are my favorites:
37 of 41 12/28/22, 9:28 AM
A Guide to the Terminal, Console, and Shell https://thevaluable.dev/guide-terminal-shell-console/
Terminal Description
☰ MENU
emulator
As you can see, I like fast and lightweight terminal emulators. This
is a very short list, but there are many more available out there.
I believe that knowing how the terminal and most common shells
work gives you an edge, especially when your terminal don’t be-
have as you want it to. This is a complex but particularly useful
tool.
Related Sources
!"Text terminals - David S. Lawyer
!"Using a 1930 Teletype as a Linux Terminal - CuriousMarc
YouTube Channel
39 of 41 12/28/22, 9:28 AM
A Guide to the Terminal, Console, and Shell https://thevaluable.dev/guide-terminal-shell-console/
Let's Connect
You'll receive each month the last article with additional resources
and updates.
I want more!
Here's how it looks
You can reply to any email if you have questions, problems, or feed-
back. I'll write back as soon as I can.
40 of 41 12/28/22, 9:28 AM
A Guide to the Terminal, Console, and Shell https://thevaluable.dev/guide-terminal-shell-console/
☰ MENU
Powered by Remark42
Configuring i3 Window
Manager: a Complete
Guide
41 of 41 12/28/22, 9:28 AM