Databases 5 min read

Build a Tiny Shell‑Based Database in Two Commands – How It Works

This article demonstrates how a functional, persistent key‑value store can be created with just two shell commands—one to append data and another to retrieve the latest entry—while explaining the underlying grep and sed techniques, performance trade‑offs, and basic indexing concepts.

ITPUB
ITPUB
ITPUB
Build a Tiny Shell‑Based Database in Two Commands – How It Works

Simple shell‑based key‑value store

Two shell functions implement a persistent key‑value database stored in a plain‑text file data.db.

db_set appends a line key,value to the file:

db_set() {
    echo "$1,$2" >> data.db
}

This log‑append write is O(1) and guarantees durability because the data is flushed to disk.

db_get retrieves the most recent value for a given key by scanning the file, selecting matching lines, taking the last occurrence, and stripping the key:

db_get() {
    grep "^$1" data.db | tail -n 1 | sed -e "s/^$1,//"
}
grep "^$1"

matches lines whose prefix equals the key argument. tail -n 1 keeps the newest entry, and sed -e "s/^$1,//" removes the key and the separating comma, leaving only the value.

Shell primitives used

grep

– pattern matching at the beginning of each line. tail – selects the last line of the filtered output. sed -e – applies a single substitution; the omission of the g flag ensures only the first match on each line is replaced, which is sufficient because each line contains a single key, prefix.

Performance characteristics

Writes are constant‑time (O(1)) because they are simple appends, similar to a write‑ahead log used by many production databases. Reads require a full scan of data.db, giving linear time O(n) where n is the number of stored entries.

Possible extensions – indexing

To improve read performance an auxiliary index can be maintained. The index stores metadata (e.g., byte offset or line number) for each key, allowing db_get to jump directly to the latest entry. Maintaining the index adds write overhead because each db_set must also update the index structure.

Illustrative images

Database command output
Database command output
Result screenshot
Result screenshot
Conceptual flow
Conceptual flow

References

Zhihu article: https://zhuanlan.zhihu.com/p/351897096

Book: Designing Data‑Intensive Applications

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

performanceindexingdatabaseShellGrepsed
ITPUB
Written by

ITPUB

Official ITPUB account sharing technical insights, community news, and exciting events.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.