Projects / Unix shell / Wrap up
Wrap up
What this shell does not do
This is a teaching shell, and it leaves out a lot:
- No
*or?wildcards, no$(...)command substitution, no here-documents, no(...)subshells or{ ...; }groups. - No
if,while,foror functions. The&&and||lists are the only control flow. - A trailing
&applies to the last pipeline only. Ina && b &,aruns in the foreground andbgoes to the background, where bash would background the whole chain. - No line editing or history. At a terminal you only get what the terminal itself offers.
- When a program is stopped, we do not save and restore the terminal's settings. A full-screen program such as an editor can leave the terminal in a state that
resetwould need to fix. - The unquoted result of an expansion is never split into words, as described in Step 3.1.
- We tested job control on Linux in a pseudo-terminal only.
Keep going
- Wildcards. After expansion, match
*and?against the file names in a directory. - Command substitution. Run
$(cmd)in a child with a pipe, read its output and substitute it. The pipe code from Phase 2 does most of the work. ifandwhile. Extend the parser to read several lines, and decide when a block ends.- History and editing. Put the terminal in raw mode and handle keys yourself, or use a crate that does.
- A
trapbuilt-in. Let scripts run code when a signal arrives.
What a real shell does differently
- bash and zsh parse a full grammar with compound commands, functions and arithmetic, where we have one flat list.
- They split and glob after expansion, expand braces and arithmetic, and handle dozens of special variables.
- They install a handler for
SIGCHLDso that they hear about a finished job at once, where we look only before each prompt. - They save and restore terminal modes per job and handle many more edge cases around the terminal handoff.
- They have decades of tests, and POSIX describes exactly what a portable script may rely on.
Check yourself
Answer each question in your head first, then open it to check.
Why can
cdnot be an external program, and what do we call commands that must run inside the shell?A program runs in its own process, and a process can only change its own working directory. The shell would learn nothing from it. Such commands are called built-ins.In a pipeline, why does every process close every pipe descriptor, including the shell itself?
The reader of a pipe sees end-of-file only when every write end of it is closed. A leftover copy of the write end in any process, the shell included, makes the reader wait forever.What does the order of
2>&1and>filechange, and why?Redirects are applied from left to right, and2>&1copies whatever descriptor 1 points to at that moment.>file 2>&1sends both to the file, while2>&1 >filesends stderr to the old stdout and only stdout to the file.Why does
echo $XafterX=1;need expansion to happen late?Expanding while reading the line would substitute$Xbefore the assignment had run. Each command has to be expanded just before it executes.Teach it back: a teammate says Ctrl-C works because the shell catches the signal and kills the running program. What do you answer?
The terminal driver, not the shell, sendsSIGINTto the whole foreground process group. The shell ignores the signal on purpose, and each job lives in its own group that holds the terminal while it runs. The shell never kills anything.
Sources
- fork(2), pipe(2) and dup2(2), the system calls behind Phase 2.
- waitpid(2), including
WNOHANG,WUNTRACEDandWCONTINUED. - setpgid(2) and tcsetpgrp(3), process groups and the terminal.
- The GNU C Library manual: Implementing a Job Control Shell, which walks through a full sample shell.
- POSIX: Shell Command Language, the specification that bash and others follow.
- The libc crate, the Rust bindings used throughout.