This project has been created as part of the 42 curriculum by leschunc, smarques.
Minishell is a project in which we have to implement a (mini 😉) shell. A shell is program able to interpret text that a user types in a window called terminal and process that information to run commands.
A command is a like an order to run a simple program or function that usually performs an instant, simple task. A shell is not only able to process singular commands but chains of commands such as a pipeline to produce more complex behaviors.
For example, echo hello displays hello on the screen, but echo hello | wc -c display 6, which is the number of characters in that line, including the new line character.
A shell is not only able to run commands, it's able to tell the user when things go wrong by producing errors, such as typing a syntactically wrong command line, such as leaving quotes open:
echo "hello instead of echo "hello". This will display the following error message in our project: minishell: syntax error, unclosed quotes detected!
Users can also redirect the output to a specific file with redirections instead of having the result displayed on the screen, or use files as input for command lines. echo hello > file writes the hello into the file and cat < file is able to read the content of the file to display them onscreen.
It's also possible for users to create an input redirection on-the-go, referred to as heredoc by typing << delimiter. Which will read the subsequent typed lines and write them into a file up until the user types the delimiter again. That will be later used to read by a command such as << delimiter cat
<< del cat
hello
world
del
The above sequence of lines typed will produce the following output, which will have been processed by cat.
hello
world
Users can interrupt certain commands and interactions with signals, by pressing ctrl + c, a user can kill a cat 😱 command that is waiting for input from the user.
Once the project is cloned, enter the root directory of the project and use make to compile the project. This will produce the minishell program. To run, type ./minishell in the command line.
Once in the minishell program, indicated by the ($ ) prompt, type command lines and then press enter.
The following builtin commands were implemented and can be tested:
- echo (can be used with the -n option to prevent the new line to to displayed)
- env
- pwd
- cd
- export (export and unset and be used to create, change and remove entries from the environment *
- unset
- exit (can take none or one numeric argument to produce an intentional error code from 0 to 255).
Builtins are commands that are not external binaries and are instead functions that are called within the shell program itself.
To use and test minishell you can combine a variety of commands, strings and symbols denominated metacharacters to operate an OS.
Try these out and combine them together to come up with your tests. Some commands are too powerful, so proceed with
echo hello
echo "hello
echo hello > file
cat < file
cat < file > anotherfile
cat anotherfile
ls
rm file anotherfile
ls
<< heredocEnd cat > resultofheredoc
hello
world
im just text!
heredocEnd
cat resultofheredoc
rm resultofheredoc
echo secretmessage > input
< input cat | cat | cat | cat | cat > output
cat output
rm input output
<< end cat | cat | cat > imatraveller
hello
world
end
cat imatraveller
rm imatraveller
The *environment is a collection of variables that are used as context for commands to run, changing them can drastically change the results of a command. They contain the PATH variable for example, which caintains information about where the external binaries are located, removing this variable will prevent the shell from finding the binaries. They will work again by either restoring or writing the absolute path to them: ls, for example, is often located in /usr/bin/ls.
Don't forget to exit with the exit command.
The singular most important piece of information, the Bash Manual https://www.gnu.org/software/bash/manual/bash.html
These two were favorites for error and exit codes references https://www.chromium.org/chromium-os/developer-library/reference/linux-constants/errnos/ https://adminschoice.com/exit-error-codes-in-bash-and-linux-os/
Also used for undestading core concepts of parsing https://pubs.opengroup.org/onlinepubs/9799919799/utilities/V3_chap02.html
AI was widely used for learning git commands, learning how to use the markdown in this README.md file, and especially for discussing concepts and deciding which better practice course to take. Although AI usage is not prohibited, we wanted to take Minishell as the singular most important opportunity in 42 to learn what it means to work on a big, complex project the "hard way", so we decided not to use AI for debugging or pasting explicit pieces of code for example. AI was used to analyze possible directions and approaches for the project, specifically in understanding how Bash processes input and the path it follows to create a structure ready to be used by the execution side of the project.
I, Leo, can safely say Minishell is my favorite project at this point in this school, I simply learned too much and still have a lot to learn.
However, the fact that the subject redirects students to bash as a reference without clear boundaries of what not to implement, often forces us into a reverse engineering path more than a mini shell one.
For example, BASH allows an input redirection to run without any command, that doesn't produce an error message and a lot of students think we should do the same. I think it should be treated as a sintax error but that freedom was taken away.
There's also the fact that error messages are sometimes printed with the " ` " character instead of " ' " as in `error' because of ***historical reasons, just to name a few of the hurdles we faced when working on it.
Peer-to-peer learning is amazing, but if the pedagogical component is not that strongly built into the subjects, then 42 should incentivize more pedagogical activities or projects for the students themselves to participate in order for them to have a better overview of the strenghts and weaknesses of the subjects and be able to build on top of them, not below.
I hope this can be used for future improvements in the Old 😭 and New Common Cores