When mordern computer was built, shell is a creative inovation at that time. People communicate with computers via shell, CLI(Command Line Interface) is not the first but very important computer user interface. For me it’s a good opportunity to learn how computer structure, operating system works.

Since I’ve learned C for 3 month. I tried some small projects, Tic-Tac-Toe, pomodoro clock, Raylib liberary, etc. All of these are built with single functionality. Because I want to explore more deep, so I chose “Build your own shell” as my first side-project, I found it from “build your own x repo” on GitHub. My own shell will not only one version, I’ll try different methods(framework) and gradually improve it.

Tutorial - Write a Shell in C I can feel that the writer wants to make this tutorial as simple as posible, there is no complicate Syntax, it’s very good for beginers who has only know basic C.

  • my_shell_v1:
    • built-in command: cd, help, and exit
    • external command: fork() + execvp()
    • do-while loop structure
   [ Start ]
       │
       ▼
┌──────────────┐
│  Read Line   │  <─── (Gets raw input via getchar)
└──────┬───────┘
       │
       ▼
┌──────────────┐
│  Split Line  │  <─── (Tokenizes string into arguments)
└──────┬───────┘
       │
       ▼
┌──────────────┐
│   Execute    │  <─── (Runs builtin command or forks subpros)
└──────┬───────┘
       │
       ▼
┌──────────────┐
│   Clean Up   │  <─── (Frees memory allocated for this loop)
└──────┬───────┘
       │
       └─ (Loop continues until 'exit' command returns 0) ──> [ End ]
  • my_shell_v2:
    • built-in command: cd, help, and exit
    • external command: fork() + execvp()
    • show current directory dynamically
    • switch-case statement and state machine
         ┌─────────────────── Empty Input / Error ──────────────────┐
         ▼                                                          │
┌─────────────────┐          Success         ┌────────────────┐     │
│  WAITING_INPUT  │─────────────────────────>│   SPLIT_LINE   │     │
└────────┬────────┘                          └───────┬────────┘     │
         ▲                                           │              │
         │                                   Success │              │
         │                                           ▼              │
         │                                   ┌────────────────┐     │
         ├─────────── Execution Done ────────│    EXECUTE     │     │
         │                                   └───────┬────────┘     │
         │                                           │              │
         │                                      If 'exit'           │
         │                                           ▼              │
         │                                   ┌────────────────┐     │
         └─────────── Reset & Clean Up ──────│      EXIT      │<────┘
                                             └───────┬────────┘
                                                     │
                                                     ▼
                                                  [ End ]