Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

6 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NanoShell

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.


Features

  • Built-in commands:

    • echo
    • pwd
    • cd
    • exit
  • Execute external programs

  • Local shell variables

  • Environment variables (export)

  • Variable expansion using $

  • Searches executables in:

    • /usr/local/bin
    • /usr/bin
    • /bin

Built-in Commands

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.

Local Variables

Create a local variable:

name=Abdullah

Use it:

echo $name

Output:

Abdullah

Local variables exist only inside NanoShell.


Environment Variables

Create an environment variable:

export city=Cairo

Use it:

echo $city

Environment variables are created using setenv() and are passed to child processes.


Variable Expansion

NanoShell replaces variables beginning with $ before executing a command.

Example:

name=John
echo $name

Output:

John

Environment variables are expanded the same way.


External Commands

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 test

Compilation

Compile with GCC:

gcc -Wall -Wextra -std=c11 nanoshell.c -o nanoshell

Running

./nanoshell

Example Session

Simple 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

System Calls / Library Functions Used

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.

Limitations

  • 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

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages