Write a Shell in C (2015)
If you want to see a pretty complete shell in a higher level language (Python, 11K lines of code), check out:<p><a href="https://github.com/oilshell/oil/" rel="nofollow">https://github.com/oilshell/oil/</a><p>And a blog here: <a href="http://www.oilshell.org/" rel="nofollow">http://www.oilshell.org/</a> (index: <a href="http://www.oilshell.org/blog/2016/11/20.html" rel="nofollow">http://www.oilshell.org/blog/2016/11/20.html</a>)<p>This article is good in that it shows that starting processes boils down to the system calls fork(), exec(), wait(), etc.<p>My shell has function calls, loops, conditionals, pipelines, subshells, command substitution, redirects, here docs, etc.  (The code has some messiness, but the architecture is clean IMO.)<p>It turns out that these features require just a few more system calls -- dup2() and fcntl() to manipulate file descriptors, pipe(), and open() / read() / write() / close().  It's a good reminder of how simple Unix is.  The complexity of the shell is basically all in string processing, and not in interfacing with the kernel  (although the interactive parts of the shell do complicate things).<p>These system calls are all available in Python, so the code ends up being comparable to what you would write in C (and I'm in the process of porting to C++ now that the architecture has settled.)