https://github.com/luckykk273/kysh
A simple shell in C.
https://github.com/luckykk273/kysh
c pipeline redirection shell
Last synced: 2 days ago
JSON representation
A simple shell in C.
- Host: GitHub
- URL: https://github.com/luckykk273/kysh
- Owner: luckykk273
- License: bsd-3-clause
- Created: 2025-04-25T00:53:50.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-05-12T08:36:24.000Z (about 1 year ago)
- Last Synced: 2025-07-19T09:07:31.552Z (10 months ago)
- Topics: c, pipeline, redirection, shell
- Language: C
- Homepage:
- Size: 35.2 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# kysh
A simple shell in C.
## Preface
This repository was originally created by following the [website tutorial](https://brennan.io/2015/01/16/write-a-shell-in-c/), and later had some additional features added (e.g. tokenizer, multiple pipes and I/O redirections). There are some known errors, but in most cases, it runs well enough. Since this repo is just intended to help with understanding some important [concepts](#key-concepts), these errors **WON'T** be fixed for now.
## Usage
### Build and run
```bash
$ make
$ ./build/kysh
kysh>
```
## Key Concepts
1. How a simple shell work
2. How to use the system call in C
- pipes (`fork()`, `pipe()`, `execvp()`, `close()`, ...)
- redirections (`open()`, `close()`, `dup2()`, ...)
3. How to tokenize the command line
- Tokenize by delimiters: `' '`, `'\t'`, `'\r'`, `'\n'`, `'\a'`
- Handle special delimiters for pipes and I/O redirection: `'|'`, `'<'`, `'>'`
NOTE: **NO** standard error supported (No `2>`, `>&`, `&>`, `2>&1`)
4. Linked list (`command_t`, `redirection_t`)
## Tests
If someone wants to see the results only, run `kysh` and enter the commands manually.
NOTE: The behaviors in `kysh` should be the **SAME** as in `Bash`
If someone wants to see the details of how `kysh` works, run the test functions in `tests/test.c`.
### Run the test functions
```bash
$ cd tests
$ make
$ ./build/test
```
### Run the commands manually
**Multiple pipes only**
```bash
kysh> ls -al | rev | nl | cat -e
```
**Multiple pipes with builtin function**
```bash
kysh> cd .. | echo hello | echo world
kysh> echo hello | cd .. | echo world
kysh> echo hello | echo world | cd ..
```
**Multiple redirections only**
```bash
kysh> echo hello > out1 > out2
kysh> ls -l > file1 -a > file2
kysh> grep main < ./src/main.c > out
```
**Multiple redirections with multiple pipes**
```bash
kysh> echo Kyle 87 > in1.txt | echo Dog 88 > in2.txt | grep Kyle > out.txt < in2.txt < in1.txt
```
**Error catch**
```bash
kysh> cd..
kysh> cd.. | ls
```
**Arbitrary whitespaces between commands and delimiters**
```bash
kysh> echo hello | ls
kysh> echo hello| ls
kysh> echo hello> hello.txt| ls
kysh> echo hello >hello.txt |ls
kysh> ls -al |rev | nl |cat -e
```
## Reference
1. [Tutorial - Write a Shell in C](https://brennan.io/2015/01/16/write-a-shell-in-c/)
2. [GitHub - lsh](https://github.com/brenns10/lsh)
3. [GitHub Gist - pipex.c](https://gist.github.com/iomonad/a66f6e9cfb935dc12c0244c1e48db5c8)
4. [CS 702 - Operating Systems - Spring 2005 - Using dup2 for I/O Redirection and Pipes](https://www.cs.loyola.edu/~jglenn/702/S2005/Examples/dup2.html)
4. [GNU Bash Manual - 3.6 Redirections](https://www.gnu.org/software/bash/manual/html_node/Redirections.html)