A simple Unix-like shell implemented in C using POSIX system calls. NanoShell supports built-in commands, execution of external programs, local variables, environment variables, and variable expansion.
-
Built-in commands:
echopwdcdexit
-
Execute external programs
-
Local shell variables
-
Environment variables (
export) -
Variable expansion using
$ -
Searches executables in:
/usr/local/bin/usr/bin/bin
| Command | Description |
|---|---|
echo [text] |
Prints text to the terminal. |
pwd |
Prints the current working directory. |
cd [directory] |
Changes the current working directory. If no directory is given, changes to /home. |
exit |
Terminates the shell. |
Create a local variable:
name=AbdullahUse it:
echo $nameOutput:
Abdullah
Local variables exist only inside NanoShell.
Create an environment variable:
export city=CairoUse it:
echo $cityEnvironment variables are created using setenv() and are passed to child processes.
NanoShell replaces variables beginning with $ before executing a command.
Example:
name=John
echo $nameOutput:
John
Environment variables are expanded the same way.
If a command is not built into the shell, NanoShell searches for the executable in:
/usr/local/bin/
/usr/bin/
/bin/
Examples:
ls
cat file.txt
mkdir testCompile with GCC:
gcc -Wall -Wextra -std=c11 nanoshell.c -o nanoshell./nanoshellSimple Shell: Enter your command $ pwd
/home/user
Simple Shell: Enter your command $ echo Hello
Hello
Simple Shell: Enter your command $ name=Abdullah
Simple Shell: Enter your command $ echo $name
Abdullah
Simple Shell: Enter your command $ export city=Cairo
Simple Shell: Enter your command $ echo $city
Cairo
Simple Shell: Enter your command $ ls
Desktop Documents Downloads
Simple Shell: Enter your command $ cd Documents
Simple Shell: Enter your command $ pwd
/home/user/Documents
Simple Shell: Enter your command $ exit
Good Bye
| Function | Purpose |
|---|---|
fork() |
Creates a child process. |
execve() |
Executes external programs. |
wait() |
Waits for child process termination. |
getcwd() |
Gets the current working directory. |
chdir() |
Changes the current working directory. |
write() |
Writes output to the terminal. |
setenv() |
Creates or updates an environment variable. |
getenv() |
Retrieves an environment variable. |
fgets() |
Reads user input. |
-
Supports one variable expansion per command.
-
Stores local variables in a fixed-size table.
-
Maximum command length is limited by the input buffer.
-
Does not support:
- Pipes (
|) - Input/output redirection (
<,>,>>) - Background execution (
&) - Wildcards (
*) - Command substitution
- Aliases
- Shell scripting
- Pipes (