This project is about creating a simple shell. Yes, your own little bash. You will learn a lot about processes and file descriptors.
The Minishell program aims to replicate the essential features of a Unix shell, allowing users to enter and execute commands as they would in a typical terminal session.
- Description: Provides an interactive terminal environment where you can execute shell commands.
- Key Features:
- Supports command execution, input/output redirection, heredoc, pipes, conditional execution, variable expansion, wildcard and quoting.
- Operates in both interactive and non-interactive modes.
- Handles interruptions like
CTRL-C
,CTRL-D
orCTRL-\
. - Implements built-in commands including
echo
,cd
,pwd
,export
,unset
,env
, andexit
.
- Usage: Run
./minishell
and enter commands in the prompt as you would in a standard shell.
Clone the Repository:
git clone https://github.com/J0hann3/Minishell.git
cd Minishell
make
- Run the program
./minishell
without any arguments. - Once launched, the program will display a prompt and wait for you to enter commands.
- Type each command and press ENTER to execute.
CTRL-C
stops the current command or displays a new prompt.- Exit the program by typing
exit
or pressingCTRL-D
.
./minishell
minishell$> echo Hello, world!
Hello, world!
minishell$> ls | grep minishell > output.txt
minishell$> echo s*
src suppr.valgrind
minishell$> export HELLO="hello world"
minishell$> echo "$HELLO"
hello world
minishell$> echo '$HELLO'
$HELLO
minishell$> echo -n "hello " || echo bad && echo world
hello world
minishell$> exit
- Run a command by piping it to
./minishell
.
echo 'ls' | ./minishell
img includes Makefile minishell obj README.md src
- Recreated the following built-in commands:
echo
with option-n
pwd
with no optionsenv
with no options or argumentscd
with only a relative or absolute pathexport
with no optionsunset
with no optionsexit
with no options
- Pipes (
|
) redirect the output of one command to the input of another. - Handles the use of pipes (
|
) to chain commands together. - Minishell supports multiple pipes in the same command line and uses fork processes to execute commands in parallel.
- Waits a process.
- Executed binaries must be waited in the execution flow by the main process.
- At the end of each pipeline,
waitpid
waits for all processes created within that pipeline.
WIFEXITED(status)
: True if child exited normally.WEXITSTATUS(status)
: Returns code when child exits.
WIFSIGNALED(status)
: True if child exited because a signal was not caught.WTERMSIG(status)
: Returns code of the terminating signal.
WIFSTOPPED(status)
: True if child is stopped.WSTOPSIG(status)
: Returns code of the stop signal.
- Returns a command's exit status that is provided by the
waitpid
, or 128 + n if terminated by signal n. - The exit status of a pipeline is the exit status of the last command.
- For commands using
&&
and||
, the exit status is that of the last command executed.
- Function that reads from the standard input.
- Keeps a history of the command entered in the program.
- Supports navigating through command history using the arrow keys.
- Uses binary trees to structure command execution and establish command priorities.
- Implements priorities with parentheses (no sub-shells implemented).
- Use
dup
anddup2
to manage input/output redirection. - Create processes to handle multi-heredoc.
- Redirection are processed in the order they appear, from left to right.
./minishell
minishell$> ls | grep src > outfile > outfile1
minishell$> ls | grep src >> outfile >> outfile1
minishell$> < infile grep obj | grep src > outfile
minishell$> cat <<hello <<world
> bad
> hello
> good
> world
good
The main
function operates as follows:
init env
while (!exit)
readline
if (heredoc)
read heredoc
history
parse
get PATH
exec command