Skip to content

mike-k-git/42_minishell

Repository files navigation

This project has been created as part of the 42 curriculum by mkugan and uwettasi.

minishell — As beautiful as a shell

Description

Minishell is a Unix shell implementation written in C, built as part of the 42 curriculum. The goal is to recreate core Bash behaviour: parsing and executing commands, managing processes and file descriptors, handling signals, and implementing a set of built-in commands from scratch.

The shell features a full lexer/parser pipeline that builds an Abstract Syntax Tree (AST) from user input. Execution traverses the AST to handle simple commands, pipelines, logical operators, subshells, and I/O redirections.

Mandatory features:

  • Interactive prompt with command history (readline)
  • Command resolution via PATH, relative, or absolute paths
  • Single (') and double (") quote handling
  • I/O redirections: <, >, >>, and heredoc <<
  • Pipes (|) connecting command output to the next command's input
  • Environment variable expansion ($VAR) and exit status expansion ($?)
  • Signal handling: ctrl-C (new prompt), ctrl-D (exit), ctrl-\ (ignored)
  • Built-in commands: echo [-n], cd, pwd, export, unset, env, exit

Bonus features:

  • Logical operators && and || with correct short-circuit evaluation
  • Parentheses (...) for subshell grouping and operator precedence
  • Wildcard * expansion in the current working directory

Instructions

Dependencies

  • gcc (or any C99-compatible compiler)
  • GNU readline library

On macOS (with Homebrew):

brew install readline

On Debian/Ubuntu:

sudo apt-get install libreadline-dev

Build

git clone https://github.com/mike-k-git/42_minishell minishell
cd minishell
make

This compiles libft first, then the shell binary minishell.

Rule Effect
make / make all Build minishell
make clean Remove object files
make fclean Remove objects and binaries
make re Full rebuild

Run

./minishell

The shell starts in interactive mode and displays a prompt. It behaves like a restricted Bash — use it as you would any shell.

Examples:

# Pipelines and redirections
ls -la | grep ".c" > sources.txt

# Heredoc
cat << EOF
hello world
EOF

# Logical operators and subshells
(echo "start" && make) || echo "build failed"

# Wildcard expansion
echo *.c

# Environment variables
export MY_VAR=hello
echo $MY_VAR
echo $?

Resources

References

AI Usage

AI tools were used in the following ways during this project:

  • Clarifying concepts: asking about POSIX signal semantics, waitpid flag behaviour, and edge cases in quote handling that the bash manual describes ambiguously.
  • Debugging assistance: describing unexpected output and asking for likely causes (e.g. pipe fd leaks in multi-stage pipelines, heredoc signal interaction).
  • Code review: reviewing specific functions for memory leaks and discussing whether a given approach to glob expansion was correct.
  • Documentation: drafting this README based on the project subject requirements.

All AI-generated content was reviewed, tested, and understood before being incorporated. Final decisions, implementations, and responsibility for correctness remain with the authors.

About

Unix shell implementation in C — bash-like with pipes, redirections, heredocs, and AST-based execution

Topics

Resources

Stars

Watchers

Forks

Contributors