Skip to content

yahya-debug/codecrafters-shell-go

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

118 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

progress-banner

Build Your Own Shell — Go

A POSIX-flavored interactive shell built from scratch in Go as part of the CodeCrafters "Build Your Own Shell" challenge. Every feature — from raw terminal input to pipeline execution — is hand-rolled with no third-party shell libraries.


Demo

$ echo "hello world"
hello world
$ ls | grep go
go.mod
go.sum
$ cat file.txt > out.txt 2> err.txt
$ sleep 5 &
[1] 12345
$ jobs
[1]+  Running    sleep 5 &
$ history 3
  8  ls | grep go
  9  cat file.txt > out.txt 2> err.txt
  10  sleep 5 &
$ exit

Features

Core Shell

  • REPL loop with $ prompt
  • Raw-mode terminal — full keystroke control via golang.org/x/term
  • Cursor-aware line editing (move, insert, delete at any position)
  • Bell feedback on invalid operations

Input Parsing

  • Single-quoted strings (no interpretation)
  • Double-quoted strings with escape sequences
  • Backslash escaping outside quotes
  • Shell variable expansion: $VAR and ${VAR}
  • && sequential AND chaining
  • & background execution

I/O

  • | multi-stage pipelines
  • > / >> stdout redirection (overwrite / append)
  • 2> / 2>> stderr redirection (overwrite / append)

Built-in Commands

  • echo [-n] [-e] [-E] — with escape sequence interpretation
  • cd [path|~] — with home directory support
  • pwd
  • type — identifies builtins vs external executables with full path
  • history [-r/-w/-a] [file] — session history with file persistence
  • jobs — lists background jobs with +/- markers and Running/Done status
  • complete -C <program> <command> — programmable completion
  • declare VAR=val / -p VAR — shell variables
  • exit — flushes history to $HISTFILE

Tab Completion

  • Complete commands (builtins + PATH executables)
  • Complete file/directory paths (appends / for dirs)
  • Longest-common-prefix completion on multiple matches
  • Double-Tab to list all candidates
  • Programmable completion via -C external programs (COMP_LINE / COMP_POINT)

History

  • Up/Down arrow navigation
  • Load from $HISTFILE on startup, save on exit
  • history -r/-w/-a for manual file management

Background Jobs

  • cmd & runs command asynchronously
  • Goroutine-based reaping via a doneCh channel
  • jobs with most-recent (+) and second-most-recent (-) markers

Project Structure

app/
├── main.go          # REPL loop, built-in dispatch
├── ReadLine.go      # Raw terminal I/O, cursor control, tab completion
├── ParseInput.go    # Single-pass tokenizer: quotes, escapes, operators, variables
├── PipeLine.go      # Multi-stage pipe execution
├── ExternalComm.go  # External command runner + I/O redirection
├── jobs.go          # Background job control
├── History.go       # History navigation and file persistence
├── comlete.go       # Programmable completion (-C) support
├── cd.go            # cd builtin
├── echo.go          # echo builtin with -n/-e/-E flags
├── variables.go     # Shell variable store (declare)
└── yahya_library.go # Generic MergeSort, Binary Search, Pair[T,U]

Running Locally

Requirements: Go 1.25+

./your_program.sh

Submit to CodeCrafters:

git push origin master

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors