Welcome to Minishell, a simplified version of the Unix shell, built as a project to understand how shells work under the hood. This shell aims to replicate some of the basic functionalities of bash
, including command execution, pipelines, redirections, and built-in commands, while adhering to specific project requirements.
- Display a prompt while waiting for user input.
- Command history navigation using arrow keys.
- Execution of commands with absolute, relative, or
PATH
-resolved paths. - Environment variable expansion, including special variables like
$?
. - Handle single and double quotes as in
bash
. - Input/output redirections (
<
,>
,>>
,<<
). - Command pipelines using the pipe (
|
) operator. - Built-in commands:
echo
,cd
,pwd
,export
,unset
,env
, andexit
. - Proper signal handling for
ctrl-C
,ctrl-D
, andctrl-\
.
- GCC compiler
- GNU Readline library - For Debian/Ubuntu-based Systems
sudo apt-get install libreadline-dev
- Make
-
Clone the Repository
git clone https://github.com/SaraitHernandez/minishell.git
-
Navigate to the Project Directory
cd minishell
-
Compile the Program
make
This will generate the
minishell
executable in the root directory.
Start the shell by running the executable:
./minishell
You should see a prompt like:
minishell$
Now you can start typing commands as you would in a regular shell.
-
Running an External Command
minishell$ ls -la
-
Using a Built-in Command
minishell$ cd /path/to/directory
-
Pipelines
minishell$ ls -la | grep minishell | wc -l
-
Redirections
-
Output Redirection:
minishell$ echo "Hello, World!" > hello.txt
-
Input Redirection:
minishell$ wc -w < hello.txt
-
Append Output:
minishell$ echo "This is appended text." >> hello.txt
-
Heredoc:
minishell$ cat << EOF > This is a heredoc input. > EOF
-
-
Environment Variable Expansion
minishell$ echo "Current user: $USER"
-
Exiting the Shell
minishell$ exit
- Usage:
echo [-n] [string ...]
- Options:
-n
: Do not output the trailing newline.
- Usage:
cd [directory]
- Changes the current directory to
directory
.
- Usage:
pwd
- Prints the current working directory.
- Usage:
export [name[=value] ...]
- Sets environment variables.
- Usage:
unset [name ...]
- Unsets environment variables.
- Usage:
env
- Prints the list of environment variables.
- Usage:
exit [n]
- Exits the shell with a status of
n
.
ctrl-C
(SIGINT
): Displays a new prompt on a new line.ctrl-D
(EOF): Exits the shell.ctrl-\
(SIGQUIT
): Does nothing.
The project is organized into directories and files to enhance modularity and maintainability.
minishell/
├── Makefile
├── README.md
├── src/
│ ├── main.c
│ ├── init_shell.c
│ ├── input.c
│ ├── lexer.c
│ ├── expand_variables.c
│ ├── parser.c
│ ├── parser_utils.c
│ ├── executor.c
│ ├── exec_utils.c
│ ├── redirection.c
│ ├── pipes.c
│ ├── signals.c
│ ├── cleanup.c
│ ├── globals.c
│ ├── builtins/
│ │ ├── builtin_echo.c
│ │ ├── builtin_cd.c
│ │ ├── builtin_pwd.c
│ │ ├── builtin_export.c
│ │ ├── builtin_unset.c
│ │ ├── builtin_env.c
│ │ └── builtin_exit.c
│ ├── environment/
│ │ ├── environment.c
│ │ └── environment_utils.c
│ └── utils/
│ ├── utils_strings.c
│ ├── utils_memory.c
│ └── utils_errors.c
├── includes/
│ └── minishell.h
└── libft/
└── [Your libft files or linked library]
- Automates the compilation process.
- Contains rules to build, clean, and rebuild the project.
- The document you're reading now.
- Provides an overview and instructions.
-
Core Source Files
main.c
- Entry point of the shell program.
init_shell.c
- Initializes the shell environment and signal handlers.
input.c
- Handles user input and command history.
lexer.c
- Tokenizes the input command line.
expand_variables.c
- Expands environment variables in tokens.
parser.c
- Parses tokens into an abstract syntax tree (AST).
parser_utils.c
- Utility functions for the parser.
executor.c
- Executes commands based on the AST.
exec_utils.c
- Utility functions assisting in command execution.
redirection.c
- Manages input/output redirections.
pipes.c
- Handles piping between commands.
signals.c
- Manages signal handling for the shell.
cleanup.c
- Frees allocated resources before exiting.
globals.c
- Contains the allowed global variable for signal indication.
-
Built-in Commands (
builtins/
)- Each built-in command is implemented in its own file for clarity and modularity.
builtin_echo.c
builtin_cd.c
builtin_pwd.c
builtin_export.c
builtin_unset.c
builtin_env.c
builtin_exit.c
-
Environment Management (
environment/
)- Manages environment variables for the shell.
environment.c
- Functions to manipulate environment variables.
environment_utils.c
- Utility functions for environment management.
-
Utilities (
utils/
)- General-purpose helper functions used across the project.
utils_strings.c
- String manipulation functions.
utils_memory.c
- Memory allocation and management functions.
utils_errors.c
- Error reporting and handling functions.
- Contains all header files.
minishell.h
- Main header file with structure definitions, function prototypes, and macros.
- Your custom library of standard functions (if applicable).
- Include or link your
libft
library here.
While minishell
replicates many features of bash
, there are some limitations:
- No Wildcard Expansion
- Patterns like
*.c
won't be expanded automatically.
- Patterns like
- Limited Error Messages
- Error messages may not be as descriptive as those in
bash
.
- Error messages may not be as descriptive as those in
- No Job Control
- Background job management (
&
,fg
,bg
, etc.) is not implemented.
- Background job management (
- No Advanced Shell Features
- Features like command substitution, shell scripting, or functions are not supported.
- No Support for Special Characters
- Characters like backslash (
\
) or semicolon (;
) are not interpreted.
- Characters like backslash (
Feel free to contact us for any questions or suggestions!
Thank you for using Minishell! We hope this project helps you understand the inner workings of a shell and provides a solid foundation for further exploration.
- Contributions: If you'd like to contribute to this project, please fork the repository and submit a pull request.
- License: This project is for educational purposes and may not be used for commercial applications.
- Acknowledgments: Thanks to the resources and community that provided guidance throughout this project.