Completed "Build Your Own Shell" Challenge
I completed the Codecrafters’ “Build Your Own Shell” challenge in Go using only the standard library. Codecrafters is a decent platform for learning by building real-world projects, and I was comfortable using the tooling they provide.
The challenge was easy to follow, it felt like solving sequential LeetCode problems from start to finish, and there was no hidden edge cases or any vague descriptions. However, in part Builtin completion #QP2, they suggested to use a third party library for readline which is chzyer/readline. chzyer/readline has zsh-style completion, where you can Tab to choose between the suggestions, not like bash-style where the suggestions are printed to stdout. This was misleading, because the challenge build a bash-like shell, not zsh, and I spent time removing zsh features from chzyer/readline.
I dropped chzyer/readline and did not look for any third party libraries that implement bash-like readline. I restarted the challenge and built everything using the standard library, and that’s when I learned the most. An implementation of readline will abstract the learning process. You will learn the library, not how it works under the hood.
I’ve implemented everything using the standard library, and I learned a couple of cool things about terminals:
- The terminal can be in raw or cooked mode, and for the shell we wanted the terminal to be in raw mode.
- Escape sequence for actions like the up arrow, where it sends the bytes
\x1b[A.
The hardest part was implementing pipe redirection and background jobs. I faced a deadlock :) implementing pipe redirection, and that’s because I forgot to clear the stdin when piping, and the goroutine kept hold of it, which resulted in a deadlock.
It was fun building GoSH, and I was happy building it using Codecrafters, because it gave me confidence that my code was right and showed me that test-driven development is important.
If you want to do the challenge, I suggest building it without using any third party libraries to get the most out of it, and please do not use AI.
The code for goSH can be found at codeberg.org/raphaeltannous/goSH under the MIT license.
Happy Goding!
