42Cursus-Minishell

Everything you need to know to build your own Shell … As beautiful as a Bash!

Achrafbelarif
3 min readNov 21, 2021

Bash is a command language written by Brian Fox for the GNU Project as a free software, it has been used as the default login shell for most Linux distributions.

Bash was the default shell in all versions of Apple macOS prior to the 2019 release of macOS Catalina, which changed the default shell to zsh.

Bash is also a command processor that typically runs in a text window where the user types commands that cause actions, it supports wildcard matching, piping, here documents, command substitution, variables (That’s exactly what our minishell must do).

YEEES, a little BASH in C !

how should our minishell behave?

  • Show a prompt when waiting for a new command.
  • Have a working History.
  • Search and launch the right executable (based on the PATH variable or by using relative or absolute path).
  • Implement the builtins:
    echo
    cd
    pwd
    export
    unset
    env
    exit
  • inhibit all interpretation of a sequence of characters.
  • inhibit all interpretation of a sequence of characters except for $.
  • Redirections:
    < should redirect input.
    > should redirect output.
    << read input from the current source until a line containing only the delimiter is seen.
    >> should redirect output with append mode.
  • Pipes | The output of each command in the pipeline is connected via a pipe to the input of the next command.
  • Semicolons ; in the command line must separate the commands.
  • Environment variables ($ followed by characters) should expand to their values.
  • $? should expand to the exit status of the most recently executed foreground pipeline.
  • ctrl-C ctrl-D ctrl-\ should work like in bash.
  • The code should not produce leaks.

Allowed Functions: readline, rl_clear_history, rl_on_new_line, rl_replace_line, rl_redisplay, add_history, printf, malloc, free, write, access, open, read, close, fork, wait, waitpid, wait3, wait4, signal, sigaction, kill, exit, getcwd, chdir, stat, lstat, fstat, unlink, execve, dup, dup2, pipe, opendir, readdir, closedir, strerror, perror, isatty, ttyname, ttyslot, ioctl, getenv, tcsetattr, tcgetattr, tgetent, tgetflag, tgetnum, tgetstr, tgoto, tputs.

Expected Behavior :

Firstly let's explore bash’s major components:

input processing, parsing, the various word expansions, other command processing, and command execution from the pipeline perspective and other stuff ...

Bash Component Architecture

The BASH/Minishell Life cycle is defined as an Infinite loop:

  1. Read line from stdin:
    . used functions:
    char *readline(const char *prompt): will read a line from the terminal and return it, using prompt as a prompt.
    add_history : allows using directional keys and .
  2. Lexer(Lexical Analyzer): checks the syntax of the commands based on BASH GRAMMAR and puts the characters together into words called “TOKENS”.
  3. Parser: that processes the tokens according to grammar and builds the “COMMAND TABLE”.

4. Execution: Execute the line.

--

--