knaxuslearn · build · ship

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, for or functions. The && and || lists are the only control flow.
  • A trailing & applies to the last pipeline only. In a && b &, a runs in the foreground and b goes 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 reset would 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.
  • if and while. 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 trap built-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 SIGCHLD so 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.

  1. Why can cd not 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.
  2. 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.
  3. What does the order of 2>&1 and >file change, and why?
    Redirects are applied from left to right, and 2>&1 copies whatever descriptor 1 points to at that moment. >file 2>&1 sends both to the file, while 2>&1 >file sends stderr to the old stdout and only stdout to the file.
  4. Why does echo $X after X=1; need expansion to happen late?
    Expanding while reading the line would substitute $X before the assignment had run. Each command has to be expanded just before it executes.
  5. 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, sends SIGINT to 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