The trip in one picture
The diagram below follows one command from the moment a key goes down to the moment the next prompt appears. The arrows at the top are input; the arrows near the bottom are the output path back to your screen. Each box is a stage that the next lesson sections describe in more detail.
From keypress to the pty
When you press a key, the operating system delivers the keystroke to the window that has focus. The terminal emulator is an ordinary graphical program. It does not run your command. It holds the master end of a pseudo-terminal pair, and it writes the bytes that represent your keys into that master. Those bytes are not yet visible to bash.
Between the master and the slave sits the line discipline, a piece of kernel code that processes the byte stream. It can echo the characters back so you see them, let you edit the line with Backspace, and hold the input back until you press Enter. Only then does the kernel make the completed line readable on the slave end, which is the terminal device that the shell and its children use as their standard input.
Bash reads and runs the line
Bash has been sitting in a read() call on file descriptor 0, which is the slave end. When the line arrives, bash does not simply hand it to a program. It performs the shell operation described in the next unit: it breaks the text into words and operators, parses the structure, expands variables and globs, removes quotes, and sets up redirections. Only after that does it decide what to run.
For an external program such as ls, bash searches $PATH for a matching executable file. Then it calls fork(), which creates a child process that is a copy of bash. In the child, bash applies the redirections and calls execve() with the program path and its arguments. The execve() call replaces the child memory image with the new program. The process ID stays the same, which is why the program that runs is still "the child" of bash.
Bash does not exit or run the program itself. It is the parent, so it calls a wait function such as waitpid() and blocks until the child finishes. The reason for forking first is that the shell must survive. If bash called execve() directly, the shell would be replaced and the command would be the last thing that happened in that terminal.
The program makes system calls
ls is a compiled C program. It cannot open a directory, read its entries, or write text to a terminal by itself. Each of those actions is a system call: a request from user code to the kernel, made through a controlled entry point. The kernel checks the request, performs the work, and returns a result or an error number. A single ls run makes a few dozen of these calls, including opening shared libraries, opening the directory, reading entries, and writing to file descriptor 1.
You can watch the boundary with strace, which records system calls made by a process. The filter below shows only the execve call, so the first line is the moment the child becomes ls.
strace -e trace=execve ls /workspaceThe first line of output looks like execve("/bin/ls", ["ls", "/workspace"], ...) = 0. The exact path depends on where ls is installed, and strace writes its report to standard error, so it appears on the same terminal as the listing. The final line, +++ exited with 0 +++, tells you the program ended normally.
Output travels back
When ls writes a line to file descriptor 1, the bytes go into the slave. The line discipline applies output processing first. By default it converts each newline into a carriage return followed by a newline, which is why the cursor returns to the left margin. The bytes then appear on the master side, where the emulator reads them.
The emulator does not treat the bytes as a simple string. It interprets control sequences: colors, cursor moves, and line clears. It then draws the result in its window. This is also why a program that prints escape sequences to a file looks like noise when you cat that file in a plain terminal.
Exit status and the next prompt
When the child exits, the kernel records its exit status and wakes up bash. Bash stores the status in the special parameter $?, and the next prompt is simply more bytes that bash writes to the slave. The status uses a simple convention: 0 means success, 1 to 125 mean the program reported a failure, 126 means the file was found but not executable, 127 means the command was not found, and 128 plus N means the program was killed by signal N.
ls /nope; echo "status=$?"This prints an error from ls and then status=2, because ls reports an inaccessible path with exit status 2. The same pattern explains the exit status 130 you get after pressing Ctrl-C at a running command: 130 is 128 plus 2, and signal 2 is SIGINT.
Hands-on: follow the stages
Start by confirming where your shell lives. The first command prints the terminal device. The second prints the process ID of the shell. The third lists processes with their parent IDs, so you can see that ps is a child of bash.
tty
echo $$
ps -o pid,ppid,tty,commIn the lab, tty prints /dev/pts/0. The PIDs you see will differ from run to run. In the ps output, the PPID column of the ps line matches the PID that echo $$ printed, and the TT column shows pts/0 for both processes.
The lab has no man pages installed. Use --help for programs, help for shell builtins, and the online pages at man7.org for system calls and the rest of the reference material.
Finally, read the lines of a single command in order. Keypress, emulator, master, line discipline, slave, bash, fork, execve, system calls, output, exit status, prompt. Every later lesson in this module zooms into one of those boxes, and the rest of the course uses the same picture when something goes wrong.