Skip to content

Maybe1or0/42sh

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

359 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Language Standard Build Status Maintained

   __ __ ___        __
  / // /|__ \ _____/ /_
 / // /___/ // ___/ __ \
/__  __/ __/(__  ) / / /
  /_/ /____/____/_/ /_/

42sh is a POSIX-oriented Unix shell written in C99.

The project is built around a classic shell pipeline: input handling, lexing, parsing, AST execution, expansions, redirections, and process management. The implementation is split into focused modules and backed by both functional and unit tests.

Highlights

  • Input from standard input, script file, or inline command with -c
  • Parsing support for command lists, pipelines, && / ||, negation, subshells, blocks, if, case, for, while, until, and function declarations
  • Builtins: cd, echo, true, false, export, unset, exit, break, continue, .
  • Variable expansion, special parameters ($?, $$, $#, $@, $*, positional parameters), and command substitution with $(...)
  • Redirections including >, >>, >|, <, <>, >&, and <&
  • Autotools-based build, clang-format checks, and CI workflows for build, tests, coverage, and forbidden-function scanning

Repository Layout

Path Role
42sh/ Main project directory
42sh/src/ Shell source code
42sh/src/lexer/ Input backend and lexer
42sh/src/parser/ Grammar and AST construction
42sh/src/execution/ Runtime, expansions, variables, functions
42sh/src/builtins/ Builtin implementations
42sh/src/redirection/ Redirection handling
42sh/src/pipe/ Pipeline execution
42sh/src/utils/ Shared helpers and data structures
42sh/tests/functionnal/ Python functional test suite
42sh/tests/units/ Criterion-based unit tests
.github/workflows/ CI configuration
42sh.pdf Project documentation / report
Doxyfile Doxygen configuration

Requirements

Build:

  • C compiler with C99 support
  • make
  • autoconf
  • automake
  • autoconf-archive
  • libtool
  • pkg-config

Tests and coverage:

  • Python 3
  • pytest
  • Criterion
  • gcovr

Example installation on Debian or Ubuntu:

sudo apt-get update
sudo apt-get install -y \
  autoconf automake autoconf-archive libtool pkg-config \
  python3 python3-pytest gcovr libcriterion-dev

Build

cd 42sh
autoreconf -fi
./configure CFLAGS="-std=c99 -pedantic -Werror -Wall -Wextra -Wvla"
make -j

The binary is generated at 42sh/src/42sh.

Run

# Interactive mode
./42sh/src/42sh

# Execute a script
./42sh/src/42sh ./script.sh

# Execute inline code
./42sh/src/42sh -c 'echo "42sh is running"'

# Pretty-print the AST instead of executing
./42sh/src/42sh -p -c 'echo hello | cat'

# Enable debug logging
./42sh/src/42sh -l -c 'echo hello'

Test

Run the functional suite through the project entrypoint:

cd 42sh
BIN_PATH="$(pwd)/src/42sh" OUTPUT_FILE="$(pwd)/out" make check
cat out

Run functional tests together with unit coverage flow:

cd 42sh
BIN_PATH="$(pwd)/src/42sh" OUTPUT_FILE="$(pwd)/out" COVERAGE=yes make check

Run the functional tests directly with pytest:

cd 42sh
BIN_PATH="$(pwd)/src/42sh" pytest -q tests/functionnal

Generate coverage-oriented build artifacts explicitly:

cd 42sh
make coverage BIN="$(pwd)/src/42sh"

Development Notes

  • The project is built with strict warnings enabled: -Werror -Wall -Wextra -Wvla
  • Formatting in CI is checked with clang-format --dry-run --Werror
  • make check delegates to tests/check.py, which can write the success rate to OUTPUT_FILE
  • API documentation can be generated with:
doxygen Doxyfile

References

Selected references for shell behavior, standards, implementation details, tooling, and project workflow.

Standards and Shell History

Shell Resources

Open Source Shells

Build and Tooling

Engineering and Design References

License

This repository is distributed under the MIT license. See LICENSE.

Bonus: Epilogue

The Search for The Ultimate Question

Deep Thought informs the researchers that it will design a second and greater computer, incorporating living beings as part of its computational matrix, to tell them what the question is. That computer was called Earth and was so big that it was often mistaken for a planet. The researchers themselves took the apparent form of mice to run the program. The question was lost, five minutes before it was to have been produced, due to the Vogons demolition of the Earth, supposedly to build a hyperspace bypass. Later in the series, it is revealed that the Vogons had been hired to destroy the Earth by a consortium of philosophers and psychiatrists who feared for the loss of their jobs when the meaning of life became common knowledge.

Douglas Adams

Douglas Adams' View

Douglas Adams was asked many times during his career why he chose the number "forty-two". Many theories were proposed, but he rejected them all. On November 3rd, 1993, he gave an answer on alt.fan.douglas-adams:

The answer to this is very simple. It was a joke. It had to be a number, an ordinary, smallish number, and I chose that one. Binary representations, base thirteen, Tibetan monks are all complete nonsense. I sat at my desk, stared into the garden and thought "42 will do". I typed it out. End of story.

About

42sh is a POSIX-compliant Unix shell implemented in C, featuring a modular architecture with lexer, parser, and execution stages. It supports pipelines, control structures, redirections, and builtins, providing a hands-on implementation of core shell and system programming concepts.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors