DEV Community

Cover image for Building Your Own Shell in C: A Journey into Command-Line Magic
Kelvin Wahome
Kelvin Wahome

Posted on • Edited on

Building Your Own Shell in C: A Journey into Command-Line Magic

Today, I want to take you on an exciting journey into the world of command-line magic by creating your very own shell in the C programming language. Shells are an integral part of any operating system, allowing users to interact with the system using text commands. By building a simple shell from scratch, you'll gain a deeper understanding of how shells work and how they execute commands.

Lets Get in it

How does shell works?
A shell reads user input, interprets the commands, and executes them by forking child processes. The standard C library provides functions like fork(), exec(), and wait() that help us achieve this.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <string.h>

#define MAX_INPUT_LENGTH 100

int main() {
    char input[MAX_INPUT_LENGTH];
    char* args[MAX_INPUT_LENGTH];
    int status;

    while (1) {
        printf("YourShell$ ");
        fgets(input, sizeof(input), stdin);

        /*Tokenize the input*/
        char* token = strtok(input, " \n");
        int i = 0;
        while (token != NULL) {
            args[i] = token;
            token = strtok(NULL, " \n");
            i++;
        }
        args[i] = NULL; /*Null-terminate the argument list*/

        if (strcmp(args[0], "exit") == 0) {
            printf("Exiting the shell...\n");
            break;
        }

        pid_t pid = fork();
        if (pid < 0) {
            perror("Fork error");
        } else if (pid == 0) {
            /*Child process*/
            if (execvp(args[0], args) == -1) {
                perror("Execution error");
                exit(EXIT_FAILURE);
            }
        } else {
            /*Parent process*/
            wait(&status);
        }
    }

    return 0;
}

Enter fullscreen mode Exit fullscreen mode

How does this work

  1. The program sets up a loop to keep the shell running until the user types "exit."
  2. It prompts the user for input and reads the command from the standard input using fgets().
  3. The input is then tokenized into arguments using strtok() and stored in the args array.
  4. The shell checks if the command is "exit"; if so, it exits the loop.
  5. If not, it forks a child process using fork().
  6. In the child process, the command is executed using execvp(), which searches for the command in the system's PATH and runs it.
  7. The parent process waits for the child to complete using wait().

Let's do it!!

To compile it we use gcc

gcc simple_shell.c -o myshell
Enter fullscreen mode Exit fullscreen mode

Now we can execute this by :

./myshell
Enter fullscreen mode Exit fullscreen mode

Conclusion

That's it. That's how shell works. Is it not interesting how the command line works?πŸ˜ƒ
Of course, there's a lot more you can add to make it more feature-rich and robust, but this serves as an excellent starting point to dive deeper into the world of shells and command-line interfaces.

If you have any questions or want to share your experiences, drop a comment below. I'd love to hear your thoughts!

Stay curious, keep coding, and until next time, happy hacking! πŸš€

Big Thanks to ALX SE for introducing me to this fascinating world.

Top comments (1)

Collapse
 
victorpreston profile image
Victor Preston

This is great