logoalt Hacker News

Building a Shell

58 pointsby ingvetoday at 9:52 AM13 commentsview on HN

Comments

emersiontoday at 12:04 PM

Some time ago I've written an article about a particular aspect of shells, job control: https://emersion.fr/blog/2019/job-control/

lvalestoday at 11:27 AM

Building a shell is a great exercise, but honestly having to deal with string parsing is such a bother that it robs like 2/3 of the joy along the way. I once built a very simple one in Go [0] as a learning exercise and I stopped once I started getting frustrated with all the corner cases.

[0] https://github.com/lourencovales/codecrafters/blob/master/sh...

show 1 reply
austy69today at 12:36 PM

Fun read. Wonder if you are able to edit text in the shell, or if you need to implement a gap buffer to allow it?

mzstoday at 11:39 AM

Had an assignment to build a shell in a week, how hard could it be?

  controlling terminal
  session leader
  job control
The parser was easy in comparison.
rigorclawtoday at 12:05 PM

The pipe implementation section is really clean. Working through fork/exec/dup2 by hand like this is one of those exercises that makes you appreciate how much composability Unix got right. Processes that know nothing about each other just work together because they read stdin and write stdout. I built something similar years ago and the moment pipes actually worked felt like unlocking a cheat code.

zokiertoday at 11:17 AM

Bit of pedantry but I don't think traditional unix shell (like this) follows repl model; the shell is not usually doing printing of the result of evaluation. Instead the printing happens more as a side effect of the commands.

show 2 replies
leontlovelesstoday at 12:05 PM

[dead]

hristiantoday at 11:33 AM

The corner cases are exactly where the learning is though. Every time you hit one unquoted spaces, escaped characters, subshell expansion — you understand something deeper about how the OS actually works. The frustration is the point. Most developers never have to think about what the shell is actually doing between their keystrokes and the kernel.

show 3 replies