A simple UNIX command language interpreter that reads commands from either a file or standard input and executes them.
- What is a shell?
- How the shell works
- Description
- Features
- Installation
- Usage
- Built-in Commands
- Environment
- Exit Status
- Signals
- Command Execution
- Operators
- Examples
- Troubleshooting
- Contact
A shell is a program that takes the command inputs written from the the user’s keyboard and passes them to the machine to execute them through the kernel. It also verifies if the command inputs from the user are correct.
So, in general, a Shell is a user interface to use the services of a computer.
The shell follows a simple cycle:
- Read: Accept input from the user
- Evaluate: Interpret the command
- Print: Display the output
- Loop: Return to step 1
- Prints a prompt and waits for a command from the user.
- Creates a child process in which the command is checked.
- Checks for built-ins, aliases in the PATH, and local executable programs.
- The child process is replaced by the command, which accepts arguments.
- When the command is done, the program returns to the parent process and prints the prompt.
- The program is ready to receive a new command
- To exit: press Ctrl-D or enter "exit" (with or without a status).
- Works also in non interactive mode.
hsh is a simple UNIX command language interpreter developed as part of the ALX Software Engineering program. It mimics the basic functionality of the Bash shell, providing a command-line interface for users to interact with their system.
- Executes commands from files or standard input
- Supports built-in commands like
cd
,exit
,env
,setenv
, andunsetenv
- Handles command separators (
;
) and logical operators (&&
,||
) - Manages environment variables
- Supports comments (lines starting with
#
) - Works in both interactive and non-interactive modes
- GCC compiler
- Linux environment (Ubuntu 20.04 LTS recommended)
-
Clone the repository:
git clone https://github.com/hackerSa3edy/simple_shell.git
-
Navigate to the project directory:
cd simple_shell
-
Compile the program:
gcc -Wall -Werror -Wextra -pedantic -std=gnu89 *.c -o hsh
Run the shell:
./hsh
You will see a prompt where you can type commands:
ls -l
pwd
echo Hello, World!
Execute commands from a file:
./hsh < commands.txt
Or pipe commands to the shell:
echo "ls -l" | ./hsh
cd [DIRECTORY]
: Change the current directoryexit [STATUS]
: Exit the shell with a given statusenv
: Print the current environmentsetenv [VARIABLE] [VALUE]
: Set an environment variableunsetenv [VARIABLE]
: Unset an environment variable
The shell uses the following environment variables:
HOME
: The home directory of the current userPWD
: The current working directoryOLDPWD
: The previous working directoryPATH
: A colon-separated list of directories to search for commands
hsh returns the exit status of the last command executed, with zero indicating success and non-zero indicating failure.
If a command is not found, the return status is 127
; if a command is found but is not executable, the return status is 126.
All builtins return zero on success and one or two on incorrect usage (indicated by a corresponding error message).
While running in interactive mode, hsh ignores the keyboard input Ctrl+c
. Alternatively, an input of end-of-file (Ctrl+d
) will exit the program.
User hits Ctrl+d
in the third line.
./hsh
^C
^C
Upon receiving a command, hsh tokenizes it into words using spaces as delimiters. The first word is interpreted as the command, while all subsequent words are treated as arguments. hsh then proceeds with the following steps:
-
If the command doesn't begin with a slash (
/
) or dot (.
), the shell first checks its list of built-in functions. If a matching built-in is found, it is executed. -
If the command doesn't start with a slash (
/
), dot (.
), and isn't a built-in function, hsh searches for an executable file with the command name in each directory listed in the PATH environment variable. -
If the command begins with a slash (
/
) or dot (.
), or if either of the above searches succeeds, the shell executes the specified program with any provided arguments in a separate execution environment.
;
: Command separator&&
: AND logical operator||
: OR logical operator
echo Hello ; ls
# Output
Hello
file1 file2 file3
mkdir test && cd test && pwd
# Output
/home/user/test
ls /nonexistent || echo "Directory not found"
# Output
Directory not found
- Command not found: Ensure the command exists and is in your PATH
- Permission denied: Check if you have the necessary permissions to execute the file
- Syntax errors: Verify your command syntax, especially when using operators