Terminals, TTYs, Shells, and Command Syntax
A terminal window, a TTY device, and a shell are separate components. This lesson builds the execution model that prevents command-line confusion and makes later Linux automation predictable.
Learning objectives
By the end of this lesson
- Distinguish terminal emulators, TTYs, pseudo-terminals, shells, and commands.
- Explain how keyboard input travels to a shell and how program output returns to the screen.
- Identify the current shell, terminal device, process hierarchy, and session mode.
- Parse a command line into command name, options, operands, quoting, and shell operators.
- Use type, command, and ps to determine what will actually execute.
1. Five layers behind a command prompt
When you type into a graphical terminal, several layers cooperate. Treating them as one object leads to incorrect troubleshooting: changing the terminal theme does not change the shell, switching shells does not change the kernel, and closing a terminal can affect processes because the pseudo-terminal disappears.
Displays and collects text
Applications such as GNOME Terminal, Konsole, Windows Terminal, and iTerm2 render characters and keyboard input.
Provides the terminal device interface
A kernel-managed terminal device connects an interactive process to input, output, line discipline, and job-control signals.
Reads and interprets commands
Bash, dash, zsh, and other shells perform parsing, expansion, redirection, pipelines, built-ins, and program execution.
flowchart LR K["Keyboard input"] --> T["Terminal emulator"] T --> P["Pseudo-terminal pair"] P --> S["Interactive shell"] S --> B["Built-in command"] S --> E["External executable"] B --> P E --> P P --> T T --> D["Displayed output"]
2. TTYs and pseudo-terminals
The term TTY comes from physical teletype devices. Modern Linux still exposes terminal interfaces through device files. A virtual console might appear as /dev/tty1; a graphical terminal or SSH session usually uses a pseudo-terminal such as /dev/pts/0.
/dev/tty1Kernel-backed text console, often reached with a function-key combination/dev/pts/3Terminal endpoint presented to Bash, SSH, tmux, or another interactive processps as TTYTerminal associated with a session for job control and terminal-generated signals? in a TTY columnCommon for daemons, services, cron jobs, and detached processesThe tty command prints the terminal connected to standard input. It can report “not a tty” when input is redirected or when the process runs non-interactively.
3. A shell is both an interface and a language
An interactive shell provides a prompt, history, completion, job control, aliases, and other conveniences. The same shell can also execute scripts non-interactively. Shell behavior depends on the executable, invocation name, options, environment, startup files, and whether the session is interactive or a login shell.
Common interactive and scripting shell
Widely available on Linux and rich enough for course automation, arrays, functions, completion, and debugging.
Portable shell interface
Often a symbolic link to another shell. Scripts using #!/bin/sh should avoid Bash-only syntax.
Small POSIX-oriented shell
Frequently used as /bin/sh on Debian-family systems for fast system scripts.
Feature-rich interactive shell
Popular for interactive use, but its extensions and startup files differ from Bash.
$SHELL is not definitiveThe variable usually records the account’s configured login shell. It does not guarantee that the current process is that shell. Inspect the running process as well.
4. Anatomy of a command line
grep --ignore-case --line-number 'failed login' /var/log/auth.log
grep is resolved as an alias, function, built-in, hashed path, or executable found through PATH.
--ignore-case and --line-number change program behavior. Option syntax belongs to the command, not universally to the shell.
'failed login' remains one argument because single quotes suppress word splitting and most expansion.
/var/log/auth.log identifies the file on which the program operates.
The shell removes syntactic quotes before launching an external program. The program receives an argument vector, not the original command-line text. Spaces inside a quoted argument are preserved as part of that argument.
5. Shell syntax is not ordinary punctuation
;End one command and start anotherpwd; id&&Run the next command only after successmkdir lab && cd lab||Run the next command only after failuretest -f file || printf 'missing\n'|Connect standard output to another command’s standard inputprintf '%s\n' a b | sort>Redirect output to a fileuname -a > kernel.txt&Start an asynchronous background jobsleep 30 &These symbols are interpreted by the shell unless quoted or escaped. Lesson 5 develops streams, pipes, redirection, and exit-status control in detail.
6. Inspect the current interactive session
printf 'Configured login shell: %s\n' "$SHELL"
printf 'Current terminal: '
tty || true
printf '\nCurrent process and parent:\n'
ps -o pid,ppid,sid,tty,stat,comm,args -p $$ -p "$PPID"
printf '\nShell flags and mode indicators:\n'
printf 'shell flags: %s\n' "$-"
case $- in
*i*) printf 'interactive=yes\n' ;;
*) printf 'interactive=no\n' ;;
esac
shopt -q login_shell && printf 'login_shell=yes\n' || printf 'login_shell=no\n'
printf '\nCommand resolution:\n'
type -a printf
command -V cd
command -V ls
$$ is the current shell process ID in a normal interactive Bash session. $PPID is its parent. SID helps identify the session, TTY the controlling terminal, and STAT process state flags.
7. Hands-on lab: create a terminal-session report
Capture the session layers without changing system configuration. Run the report from a local graphical terminal and, when available, an SSH or tmux session. Compare the terminal device and process hierarchy.
lab="$HOME/devops-academy/linux/chapter03/lesson01"
mkdir -p -- "$lab"
report="$lab/session-report.txt"
{
printf '=== timestamp ===\n'
date --iso-8601=seconds 2>/dev/null || date
printf '\n=== identity ===\n'
id
printf '\n=== shell variables ===\n'
printf 'SHELL=%s\n' "${SHELL-}"
printf 'BASH_VERSION=%s\n' "${BASH_VERSION-}"
printf 'flags=%s\n' "$-"
printf '\n=== terminal ===\n'
tty || true
stty -a 2>/dev/null || true
printf '\n=== process chain ===\n'
current=$$
count=0
while [ "$current" -gt 1 ] && [ "$count" -lt 8 ]; do
ps -o pid=,ppid=,sid=,tty=,stat=,comm=,args= -p "$current"
current=$(ps -o ppid= -p "$current" | tr -d ' ')
[ -n "$current" ] || break
count=$((count + 1))
done
printf '\n=== command resolution ===\n'
type -a cd printf ls 2>&1
} > "$report"
less "$report"
Verification checklist
8. Common command-line misconceptions
“The terminal executes commands.”
The terminal handles display and input. The shell parses the command and either runs a built-in or starts another executable.
“Everything typed after a command is an option.”
Arguments may be options, operands, subcommands, data, or syntax interpreted by the shell before the program starts.
“/bin/sh is always Bash.”
The path can point to dash, Bash, or another compatible shell. Portable scripts must not assume Bash extensions.
“A daemon has a terminal.”
Services usually run without a controlling TTY. Their input, output, signals, and lifecycle are managed differently.
9. Knowledge check
Question 1. What is the difference between a terminal emulator and a shell?
Question 2. Why can $SHELL disagree with the shell currently running?
Question 3. What does quoting change before an external command starts?
10. Summary
A terminal emulator presents text, a TTY or PTY provides the terminal device interface, and a shell parses commands and starts work. Command lines contain shell syntax as well as arguments passed to programs. Reliable operators inspect the actual process, terminal, session mode, and command resolution rather than guessing from the prompt.
11. Further reading
- GNU Bash Reference Manual — shell operation, parsing, built-ins, invocation, and interactive shells.
- Linux manual pages for tty, pts, stty, ps, type, command, and bash.
- POSIX Shell Command Language — portable shell grammar and command execution model.
- The Linux Programming Interface documentation and Linux man-pages — sessions, process groups, terminals, and system calls.
- systemd documentation — service processes and non-interactive execution environments.
Keep the academy open
Support free, practical DevOps education.
Every lesson is designed to remain readable in a browser, downloadable from GitHub, and usable without a paid learning platform. Contributions help expand and maintain the curriculum.
0x716c4Ab160C4B66F31a28AE2448BfF68fc3a2ef0
Send only Ethereum/ERC-20 compatible assets to this address.