Skip to content

Implementation of a terminal inspired by Bash, featuring tokenization, and execution of built-in and non-built-in commands.

Notifications You must be signed in to change notification settings

oishchen42/Mshell

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

167 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

~# Minishell

Custom implementation of a terminal (a small Unix shell) with emphasis on parsing and execution.

This repository contains a compact shell written in C that implements command parsing, execution, pipes, I/O redirections, environment handling and common builtin commands. It is intended as a learning / educational project and a lightweight shell replacement for simple command-line tasks.

Table of Contents

Features

  • Tokenization and parsing of command lines with:
    • single and double quotes
    • environment variable expansion ($VAR)
    • handling of whitespace
  • Execution of:
    • external binaries (via execve)
    • pipeline chains (using pipes)
    • I/O redirections: >, >>, <
    • here-document (<<) support (if implemented)
  • Basic job of a shell environment:
    • maintains a process environment
    • builtin commands for shell management
  • Signal handling for interactive mode (e.g., Ctrl-C behavior)

The implementation focuses on parsing correctness and the execution flow (fork/exec, file descriptor management).

Requirements

  • POSIX-compatible system (Linux or macOS)
  • GCC or clang (C compiler)
  • make (Makefile provided)

Build

Clone the repository and run make:

git clone https://github.com/oishchen42/Minishell.git
cd Minishell
make

This will produce an executable (commonly named minishell — check the Makefile for the exact target).

To clean build artifacts:

make clean

Usage

Start the shell:

./minishell

You will see a prompt (depending on implementation). Type commands as you would in a normal shell:

  • Run programs: ls -la
  • Use pipes: cat file.txt | grep foo | wc -l
  • Redirect output: echo hello > out.txt
  • Append output: echo world >> out.txt
  • Redirect input: wc -l < file.txt
  • Combine pipes and redirects: grep pattern file | sort > result.txt

Exit the shell with the exit builtin or Ctrl-D (EOF) if supported.

Examples

  • Run a single command:

    /bin/ls -la
    
  • Pipelines:

    ps aux | grep minishell | awk '{print $2}'
    
  • Redirections:

    echo "hello" > hello.txt
    cat < hello.txt
    
  • Variable expansion:

    echo $HOME
    
  • Quoted strings:

    echo "a string with spaces"
    echo 'literal $HOME is not expanded in single quotes'
    

Supported Builtins

Common builtin commands implemented in this project typically include:

  • cd — change current directory
  • echo — print text
  • pwd — print working directory
  • export — set environment variables
  • unset — unset environment variables
  • env — print environment
  • exit — exit the shell

Check the source for the exact set and behavior of builtins.

Behavior & Design Notes

  • Parsing: tokens are split respecting quotes and expansions; the parser builds an execution plan (commands, args, redirections, and pipe relationships).
  • Execution: the shell forks child processes and uses execve to run external commands. File descriptors for redirections and pipes are set up before exec.
  • Signals: the shell should handle interactive signals (SIGINT, SIGQUIT) appropriately — e.g., allow interrupting running child processes without exiting the shell itself.
  • Exit codes: the shell should propagate the exit status of the last foreground command.

About

Implementation of a terminal inspired by Bash, featuring tokenization, and execution of built-in and non-built-in commands.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •