Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Nutshell

A lightweight Unix shell written in C. Nutshell supports built-in commands, external program execution, I/O redirection, background processes, and batch mode.

Features

  • Built-in commandscd, clr, dir, environ, echo, help, pause, quit
  • External commands — runs any program available on your system via fork/execvp
  • I/O redirection<, >, and >> operators
  • Background execution — append & to run a command without blocking the prompt
  • Batch mode — pass a file of commands as an argument

Building

make

The binary is placed in bin/nutshell.

To clean up:

make clean

Usage

Interactive mode:

./bin/nutshell

Batch mode:

./bin/nutshell commands.txt

Built-in Commands

Command Description
cd [dir] Change directory (no argument prints current directory)
clr Clear the screen
dir [dir] List directory contents (ls -al)
environ Print all environment variables
echo [text] Print text to stdout
help Display the user manual via more
pause Pause until Enter is pressed
quit Exit the shell

Running Commands

Internal commands

Internal commands are built directly into the shell — they run instantly without spawning a new process. Just type the command name at the prompt:

/home/user> echo Hello World
Hello World

/home/user> cd /tmp
/tmp>

/tmp> dir
total 0
drwxrwxrwt  ...

These are always available regardless of your PATH.

External commands

External commands are any programs installed on your system. Nutshell finds them via your PATH environment variable, forks a child process, and runs them with execvp. Use them exactly as you would in any other shell:

/home/user> ls -la
/home/user> grep "foo" file.txt
/home/user> python3 script.py
/home/user> gcc main.c -o main

If a command isn't found, you'll see:

nutshell: command not found: badcommand

The key difference: internal commands run inside the shell process (so cd can actually change the shell's directory), while external commands run in a separate child process and can't affect the shell's own state.

I/O Redirection

# Redirect input
sort < names.txt

# Redirect output (overwrite)
ls > files.txt

# Redirect output (append)
echo done >> log.txt

# Combined
sort < unsorted.txt > sorted.txt

The dir, environ, echo, and help built-ins support output redirection.

Background Execution

sleep 30 &
find / -name "*.log" &

The shell prints the background process PID and returns the prompt immediately.

Project Structure

nutshell/
├── src/
│   ├── nutshell.c   # Main entry point and shell loop
│   ├── nutshell.h   # Header — structs, constants, prototypes
│   └── utility.c    # Built-in commands, parsing, execution
├── bin/             # Compiled binary (generated by make)
├── manual/
│   └── readme.txt   # Full user manual (viewed via the help command)
└── makefile

Requirements

  • GCC
  • POSIX-compliant OS (Linux or macOS)

About

A lightweight Unix shell written in C

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages