Full Byte Developer.

What happens when you press Enter in a terminal

The terminal emulator feeds a shell; Bash forks, execs external commands, waits, and keeps builtins in-process.

Published here , updated · 6 sources

You type ls, press Enter, and a listing appears. Three layers did the work: the terminal emulator, the TTY device, and a shell that parsed the line then ran a built-in or an external program [1] [3]. Related: pipes and redirection, essential Linux commands part 1.

Quick start: separate built-in from external

On a draft Linux host with Bash:

bash
type cd
type ls

Output:

text
cd is a shell builtin
ls is /usr/bin/ls

Built-ins live inside the shell process [1]. ls resolves to an external file path; Bash will fork and exec for that path [1] [3].

Draw the Enter key path

The Bash Beginners Guide: when the first word is a built-in, the shell executes it without creating a new process [1]. External commands use fork then replace the child image with exec, while the parent waits for status [1] [2].

Follow fork, exec, wait with real PIDs

bash
ps -o pid,ppid,cmd -p $$
sleep 2 &
echo "background pid=$!"
wait $!
echo "sleep exit=$?"

You should see your shell's PID as PPID for a short-lived sleep child while it runs, then wait returns when that child exits [1] [2]. Background & skips the automatic wait; you call wait when you need the exit code [1].

The GNU C Library notes that after fork, both parent and child continue; collecting child status is explicit via wait family calls [2].

Environment inheritance

Fork copies the parent's environment, open file descriptors, and working directory into the child before exec replaces the program [1] [2]. That is why export affects children and why redirection on the shell line applies before the program starts.

bash
export DEMO_FBD=1
env | grep DEMO_FBD

Draft host output:

text
DEMO_FBD=1

The child env process inherited the variable [1] [2].

Parsing before execution

Per the Beginners Guide, Bash tokenizes the line, performs expansions (parameters, globs, command substitution), then applies redirections and pipes [1]. Pipes are not magic shortcuts; they wire file descriptors between processes. Detail: pipes and redirection.

Command substitution $(...) runs a nested shell or command and substitutes stdout text into the outer line before the final execution pass [1].

Job control symbols

& runs a command in the background. fg and bg move jobs; jobs lists them [3]. Each external job is still a process with its own PID [2]. Signals interact with process groups; see Ctrl+C is SIGINT for the interrupt path.

SSH and remote terminals

SSH allocates a PTY on the server when you pass -t or when sshd forces one for interactive sessions. The same Bash fork/exec story runs; only the emulator moves to your local laptop window.

What the terminal emulator actually does

The emulator translates key events into bytes on the PTY master side [3]. Control keys generate control characters; Enter typically inserts a newline the shell reads as end-of-line. The shell does not see pixels; it sees a byte stream. Resizing the window sends SIGWINCH so full-screen programs can reflow.

Line editing (readline behavior) happens in the shell before execution: history, Ctrl+A, and completion are shell features, not the terminal binary.

Subshells and grouping

Parentheses ( ... ) run commands in a subshell: another Bash instance that inherits copies of variables unless exported [3]. Pipes may run the right-hand side in a subshell depending on shell options; that detail bites when you try to mutate variables inside pipelines.

One line, multiple commands

; runs sequentially. && and || gate on exit status [3]. Each external segment in the chain can fork separately. Built-ins in the chain still avoid extra processes [1].

Mistakes that waste a week

Calling Terminal.app or Windows Terminal "Bash." The emulator is not the shell [3].

Expecting cd to fork (it does not; it mutates the shell's working directory) [1].

Background jobs always inherit your script's set -e behavior across subshell boundaries.

Forgetting that exec replaces the child without returning when successful [2].

Pipes do not create threads. They create processes (or built-in combos) with connected file descriptors [1]. They create processes (or built-in combos) with connected file descriptors [1].

Running sudo command and not realizing a new user context and often a new environment contract apply.

Aliases and functions before fork

Bash expands aliases before deciding built-in versus external [3]. A function defined in the shell also runs without exec unless it calls an external explicitly. type and command -v are your truth serum when a script behaves differently from an interactive session.

Exit codes propagate

The exit status of the last foreground command becomes $? [1]. set -e in scripts makes the shell exit on failure unless guarded. Pipelines with set -o pipefail surface failures in middle stages [3]. These rules sit entirely in the shell layer before your program's own exit codes.

Interactive vs non-interactive shells

Bash reads startup files differently when stdin is a TTY versus a script [3]. An interactive login shell loads profiles you never see in CI. That distinction explains why aliases work on your laptop but not in cron unless you define them in the script.

Heredocs and multiline input

Heredocs supply multiple lines to a command without a separate file [3]. The shell still parses the delimiter line before running the compound command, so redirection and variable expansion follow the same order as single-line input [1].

When you paste a multiline block into an interactive session, each line waits for Enter until the shell decides the compound command is complete.

Tab completion and history search stay in readline until you accept a line [3].

Wrappers such as nice, env, and time may exec another program while remaining parent processes; type shows whether the wrapper is external [1] [3].

A remote ssh host command often skips aliases you rely on interactively, so scripts should use absolute paths or export what they need [3].

Here-strings, pipes, and /dev/null only change which descriptors the child inherits; the fork/exec path is the same [1].

Tonight, run type ls, type cd, and type echo and write down which ones never fork. That list is the whole lesson in one sticky note. When you need process IDs and credentials behind the child that did fork, read what is a process; for shared-memory concurrency versus isolation, continue with threads vs processes.

Send this to the friend who thinks every command forks a new process.

Abstract illustration of a signal moving across raised platforms with teal disks, an orange ringed gate, and a U-shaped return arrow beside a faint app-window outline, no people and no readable UI text.
Enter crosses layers: gate, central platform, then a child path with a return loop.

Keep this guide

By Full Byte Developer

Filed under Dev explainers, Dev tools

Questions about this guide

Is the shell the same thing as the terminal window?

No. The terminal emulator draws the window and talks to a TTY device. The shell (often Bash) reads lines, expands them, and runs commands. Confusing the layers makes job-control bugs harder to debug.

Do all commands fork?

No. Bash built-ins run inside the shell process without creating a new process. External programs use fork then exec, then the parent usually waits.

Why does a background job skip the shell's automatic wait?

No. After fork the child is its own process. The parent usually waits for a foreground external command; a background job with `&` skips that wait until you call `wait`. Signal delivery follows process groups and job control, not a blanket forward-every-signal rule.

Where do pipes fit?

After the shell parses the line, redirections and pipes rearrange file descriptors before the commands run. See the pipes post for the operator set.

Does Enter send a newline character?

Yes. The terminal emulator typically submits a line ending (often LF) to the TTY; the shell reads until that delimiter.

Sources

Next in this stack