Skip to content

A POSIX-compliant shell implementation built in Go as part of the CodeCrafters "Build Your Own Shell" Challenge. This shell supports command parsing, built-in commands, external program execution, I/O redirection, pipelines, command history, and tab completion.

Notifications You must be signed in to change notification settings

samuellachisa/shell

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

62 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Shell Implementation in Go

progress-banner

A POSIX-compliant shell implementation built in Go as part of the CodeCrafters "Build Your Own Shell" Challenge. This shell supports command parsing, built-in commands, external program execution, I/O redirection, pipelines, command history, and tab completion.

Features

Core Functionality

  • Command Parsing: Robust parsing with support for single quotes, double quotes, and backslash escaping
  • Built-in Commands: echo, exit, pwd, cd, type, and history
  • External Programs: Execute any program available in $PATH
  • Interactive REPL: Read-Eval-Print Loop with raw terminal mode support

Advanced Features

  • I/O Redirection:
    • Standard output redirection (>, >>, 1>, 1>>)
    • Standard error redirection (2>, 2>>)
  • Pipelines: Chain multiple commands with | operator
  • Command History:
    • Persistent history saved to ~/.shell_history (or $HISTFILE)
    • Navigate history with arrow keys (↑/↓)
    • History commands: history, history N, history -r FILE, history -w FILE, history -a FILE
  • Tab Completion:
    • Auto-complete commands from built-ins and $PATH
    • Show all matches on double-tab
    • Longest common prefix completion

Quote and Escape Handling

  • Single Quotes ('...'): Preserve literal values of all characters
  • Double Quotes ("..."): Preserve literal values except \, $, ", and newline
  • Backslash Escaping: Escape special characters outside quotes or specific characters inside double quotes

Installation

Prerequisites

  • Go 1.25 or higher
  • Unix-like operating system (Linux, macOS)

Build

go build -o shell app/main.go

Run

./your_program.sh

Or directly:

go run app/main.go

Usage

Basic Commands

$ echo Hello, World!
Hello, World!

$ pwd
/home/user/current/directory

$ cd /tmp
$ pwd
/tmp

$ type echo
echo is a shell builtin

$ type ls
ls is /usr/bin/ls

Quoting and Escaping

$ echo "Hello   World"
Hello   World

$ echo 'Single quotes preserve $PATH'
Single quotes preserve $PATH

$ echo "Double quotes expand \$PATH: $PATH"
Double quotes expand $PATH: /usr/bin:/bin

$ echo Hello\ \ \ World
Hello   World

I/O Redirection

$ echo "Hello" > output.txt
$ cat output.txt
Hello

$ echo "World" >> output.txt
$ cat output.txt
Hello
World

$ ls nonexistent 2> error.log
$ cat error.log
ls: cannot access 'nonexistent': No such file or directory

Pipelines

$ echo "hello world" | tr a-z A-Z
HELLO WORLD

$ cat file.txt | grep pattern | wc -l
42

$ ls -la | grep ".go" | sort

Command History

$ history
    1  echo Hello
    2  pwd
    3  cd /tmp

$ history 2
    2  pwd
    3  cd /tmp

$ history -w myhistory.txt    # Write history to file
$ history -r myhistory.txt    # Read history from file
$ history -a myhistory.txt    # Append new commands to file

Tab Completion

$ ec<TAB>         # Completes to "echo "
$ his<TAB>        # Completes to "history "
$ l<TAB><TAB>     # Shows all commands starting with 'l'
ls  ln  less  ...

Project Structure

codecrafters-shell-go/
├── app/
│   └── main.go           # Main shell implementation
├── codecrafters.yml      # CodeCrafters configuration
├── go.mod                # Go module definition
├── go.sum                # Go dependencies checksums
├── your_program.sh       # Shell script to run the program
└── README.md             # This file

Implementation Details

Command Parsing (parseCommand)

The parser handles complex quoting and escaping scenarios:

  • Tracks quote state (single/double)
  • Handles backslash escaping with context-aware rules
  • Splits on whitespace outside quotes
  • Preserves whitespace inside quotes

Terminal Handling (readLine)

Uses raw terminal mode for advanced features:

  • Character-by-character input processing
  • ANSI escape sequence handling for arrow keys
  • Tab completion with visual feedback
  • Backspace and Ctrl+C support

Pipeline Execution (handlePipeline)

Implements multi-command pipelines:

  • Creates pipes between commands
  • Handles both built-in and external commands
  • Manages goroutines for concurrent execution
  • Properly closes pipe file descriptors

History Management

  • Loads history on startup from $HISTFILE or ~/.shell_history
  • Saves incrementally on exit
  • Supports reading/writing/appending to custom files
  • Tracks last appended index to avoid duplicates

CodeCrafters Challenge

This project is part of the CodeCrafters "Build Your Own Shell" challenge. To submit solutions:

git add .
git commit -m "Implement feature X"
git push origin master

Test output will be streamed to your terminal.

Dependencies

  • golang.org/x/term - Terminal control and raw mode support
  • golang.org/x/sys - System calls for Unix-like systems

License

This project is created for educational purposes as part of the CodeCrafters challenge.

Acknowledgments

Built as part of the CodeCrafters "Build Your Own Shell" challenge, which teaches shell internals through hands-on implementation.

About

A POSIX-compliant shell implementation built in Go as part of the CodeCrafters "Build Your Own Shell" Challenge. This shell supports command parsing, built-in commands, external program execution, I/O redirection, pipelines, command history, and tab completion.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published