Warning: Work in progress! Leave feedback on Zulip or Github if you'd like this doc to be updated.

The Unix Shell Process Model - When Are Processes Created?

OSH and YSH are both extensions of POSIX shell, and share its underlying "process model".

Each Unix process has its own memory, that is not shared with other processes. (It's created by fork(), which means that the memory is "copy-on-write".)

Understanding when a shell starts processes will make you a better shell programmer.

As a concrete example, here is some code that behaves differently in bash and zsh:

$ bash -c 'echo hi | read x; echo x=$x'
x=

$ zsh -c 'echo hi | read x; echo x=$x'
x=hi

If you understand why they are different, then that means you understand the process model!

(OSH behaves like zsh.)


Related: Interpreter State. These two docs are the missing documentation for shell!

Table of Contents
Shell Constructs That Start Processes
Pipelines myproc | wc -l
Command Sub d=$(date)
Process Sub <(sort left.txt)
Async - fork or sleep 2 &
Explicit Subshell - forkwait or ( echo hi )
FAQ: "Subshells By Surprise"
shopt -s lastpipe
Other Pipelines
Process Optimizations - noforklast
Process State
Redirects
Builtins
wait
fg
bg
trap
Appendix: Non-Shell Tools

Shell Constructs That Start Processes

Pipelines myproc | wc -l

Note that functions Can Be Transparently Put in Pipelines:

Hidden subshell:

{ echo 1; echo 2; } | wc -l

A SubProgramThunk is started for the LHS of |.

Command Sub d=$(date)

d=$(date)   

Process Sub <(sort left.txt)

diff -u <(sort left.txt) <(sort right.txt)

Async - fork or sleep 2 &

Explicit Subshell - forkwait or ( echo hi )

Explicit Subshells are Rarely Needed.

FAQ: "Subshells By Surprise"

Sometimes subshells have no syntax.

Common issues:

shopt -s lastpipe

Mentioned in the intro:

$ bash -c 'echo hi | read x; echo x=$x'
x=

$ zsh -c 'echo hi | read x; echo x=$x'
x=hi

Other Pipelines

myproc (&p) | grep foo

Process Optimizations - noforklast

Why does a Unix shell start processes? How many processes are started?

Bugs / issues

Oils/YSH specific:

Process State

Redirects

Builtins

wait

fg

bg

trap

Appendix: Non-Shell Tools

These Unix tools start processes:

Generated on Fri, 01 Nov 2024 00:43:49 +0000