Skip to content
melmut-42Public

About

A simple Unix shell implemented in C, replicating basic shell behavior with support for pipes, redirections, environment variables, and built-in commands.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

174 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

minishell Banner

minishell

A bash-like shell written in C: parsing, expansion, pipes, redirections, and builtins.

42 badge Language C Score

Table of contents


Core capabilities

  • Interactive prompt with readline-backed line editing and history
  • Executable resolution via PATH, plus absolute/relative paths
  • Single and double quote parsing, including nested and adjacent quoting
  • Variable expansion ($VAR, $?) inside double quotes and bare words
  • Pipelines (|) with correct file-descriptor wiring across arbitrarily many stages
  • Full redirection suite: <, >, >>, << (heredoc)
  • bash-compatible signal handling for Ctrl+C, Ctrl+D, Ctrl+\
  • Builtins implemented in-process (no fork for cd, export, etc., matching real shell semantics)

Architecture

main.c → readline prompt → tokenizer → parser → expansion → execution
                                                                ├── builtins   (in-process)
                                                                └── pipeline   (fork/exec/pipe/wait)
Module Responsibility
parsing/ Tokenization, quote handling, AST/command-list construction, syntax validation
execution/ Process management: fork/exec, pipe wiring, redirections, exit-status propagation
builtins/ echo, cd, pwd, export, unset, env, exit
utils/ String, memory, and environment-array helpers shared across modules
libft/ Custom C utility library, linked into every module

Commands are processed in four stages: tokenize → parse (build command structures, validate syntax) → expand (resolve $VAR/$?) → execute (fork children, wire pipes/redirections, wait).


Build & run

Requirements: gcc/cc, make, an internet connection for the first build (downloads readline 8.2)

make        # downloads + builds readline 8.2, builds libft, compiles minishell
./minishell
make clean  # remove object files
make fclean # remove object files, binary, and the vendored readline build
make re     # fclean + build

The first build takes longer since it compiles readline from source; subsequent builds are fast.


Builtins

Command Syntax Description
echo echo [-n] [text...] Print text, -n suppresses the trailing newline
cd cd [path] Change directory (relative/absolute)
pwd pwd Print the working directory
export export [VAR=value] Set or display environment variables
unset unset [VAR] Remove an environment variable
env env List environment variables
exit exit [status] Exit with an optional status code

Usage examples

minishell> echo "Goodbye, World?"
Goodbye, World?

minishell> ls -la | grep minishell > output.txt
minishell> cat << EOF | sort | uniq >> results.txt
heredoc line 1
heredoc line 2
EOF

minishell> export MY_VAR="test value"
minishell> echo "Value: $MY_VAR, Exit code: $?"
Value: test value, Exit code: 0

minishell> export USER="Melovi"
minishell> echo "'Hello $USER'" vs '"Hello $USER"'
'Hello Melovi' vs "Hello $USER"

Project structure

.
├── srcs/
│   ├── builtins/    # echo, cd, pwd, export, unset, env, exit
│   ├── execution/   # Process management and execution
│   ├── parsing/     # Tokenization and command parsing
│   ├── utils/        # Helper functions
│   └── main.c
├── includes/        # Headers and prototypes
├── libft/           # Custom C utility library
└── supps/           # Valgrind/readline suppression files

Limitations

  • No arrow-key history navigation (only readline's in-session line editing)
  • Backslash escaping is not implemented
  • No command substitution (`cmd`/$(cmd)) or wildcard/glob expansion

About

A simple Unix shell implemented in C, replicating basic shell behavior with support for pipes, redirections, environment variables, and built-in commands.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Contributors

Languages