- Posted on
- • Artificial Intelligence
Artificial Intelligence Documentation with Bash
- Author
-
-
- User
- linuxbash
- Posts by this author
- Posts by this author
-
Artificial Intelligence Documentation with Bash: Turn Your Codebase into Living Docs
Most teams know their docs are out-of-date the moment a feature ships. What if you could use the tools you already love—Bash, grep, curl—to keep documentation fresh with a thin layer of AI? In this guide, you’ll learn how to wire AI into your command line to generate and maintain accurate, auditable docs right from your repo.
You’ll get:
Why Bash + AI is a powerful combo for documentation
A minimal, provider-agnostic pattern for calling AI from shell
3–5 actionable workflows you can drop into any project
Copy-paste install commands for apt, dnf, and zypper
Why AI + Bash for Documentation?
It fights documentation drift. Pull context from code/comments/commits and let AI draft or update the docs that match the latest changes.
It’s reproducible and auditable. Prompts live in your repo and are invoked by simple shell scripts. No mystery UI steps.
It stays in your toolchain. Works headless on CI, on servers, and over SSH. No editors or plugins required.
It’s vendor-agnostic. Use a standard HTTP interface; swap providers without rewriting your workflow.
Prerequisites and Installation
You’ll need these CLI tools:
curl (HTTP requests)
jq (JSON shaping)
ripgrep (fast search/extraction)
pandoc (format conversion; optional for man pages/PDF)
inotify-tools (optional: watch and auto-regenerate)
git (diffs and hooks)
Install the essentials:
Debian/Ubuntu (apt):
sudo apt update
sudo apt install -y curl jq ripgrep pandoc inotify-tools git
Fedora/RHEL (dnf):
sudo dnf install -y curl jq ripgrep pandoc inotify-tools git
openSUSE (zypper):
sudo zypper refresh
sudo zypper install -y curl jq ripgrep pandoc inotify-tools git
Set an API key for your AI provider (example uses OpenAI; you can point the endpoint to any OpenAI-compatible server):
export OPENAI_API_KEY="sk-...your key..."
export AI_ENDPOINT="https://api.openai.com/v1/chat/completions"
export AI_MODEL="gpt-4o-mini"
Tip: Store secrets in your shell rc file or a credential manager; never commit them.
A Reusable Bash Function to Call an LLM
Drop this into scripts that need AI. It’s small, readable, and easy to swap providers.
ai_chat() {
# Usage: ai_chat "your prompt text" ["system instruction"]
local user_prompt="$1"
local system_prompt="${2:-You are a concise, accurate technical writer for Linux CLI users.}"
: "${OPENAI_API_KEY:?Set OPENAI_API_KEY}"
local endpoint="${AI_ENDPOINT:-https://api.openai.com/v1/chat/completions}"
local model="${AI_MODEL:-gpt-4o-mini}"
curl -sS "$endpoint" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-H "Content-Type: application/json" \
-d "$(jq -n \
--arg model "$model" \
--arg sys "$system_prompt" \
--arg usr "$user_prompt" \
'{
model: $model,
temperature: 0.2,
messages: [
{role:"system", content:$sys},
{role:"user", content:$usr}
]
}')" \
| jq -r '.choices[0].message.content'
}
1) Generate a README from Real Code Context
This script harvests comments, docstrings, and CLI usage from your repo, then asks the LLM to draft a crisp README. It avoids vendor directories and caps the input size.
Create scripts/ai-readme.sh:
#!/usr/bin/env bash
set -euo pipefail
# Load ai_chat (paste the ai_chat function here or source it)
ai_chat() { :; } # replace with the function above or: source "$(dirname "$0")/ai.sh"
TMP_CONTEXT="$(mktemp)"
trap 'rm -f "$TMP_CONTEXT"' EXIT
# Collect signals of intent: comments, README fragments, usage messages
rg --hidden --no-messages \
-g '!*.min.*' -g '!node_modules' -g '!.git' -g '!dist' -g '!build' -g '!vendor' \
-n -H --line-number --color never \
-e '^\s*#' -e '^\s*//' -e '^\s*/\*' -e '^\s*\*' -e '^\s*"""' -e '^\s*<!--' -e '^\s*-->' \
-e 'Usage: ' -e '--help' -e '^## ' \
| head -n 5000 > "$TMP_CONTEXT"
REPO_NAME="$(basename "$(git rev-parse --show-toplevel 2>/dev/null || pwd)")"
read -r -d '' PROMPT <<'EOF' || true
You are generating a README.md for a software project.
- Write a clear intro (what, why, who).
- Show quickstart install/run instructions.
- Document the main CLI commands and examples.
- Note configuration, environment variables, and limits.
- Keep it Linux/Bash friendly. Use fenced code blocks.
- Derive details from the provided context; do not invent features.
Context (snippets from comments, help text, and headings):
EOF
CONTENT="$(cat "$TMP_CONTEXT")"
OUTPUT="$(ai_chat "$PROMPT
$CONTENT
Project name: $REPO_NAME
")"
printf "%s\n" "$OUTPUT" > README.md
echo "README.md generated."
Run it:
bash scripts/ai-readme.sh
Optional: auto-regenerate on file changes during development:
inotifywait -m -e modify,create,delete --exclude '(\.git|node_modules|dist|build|vendor)' -r . \
| while read -r _; do bash scripts/ai-readme.sh; done
2) Stop Docs Drift with a Pre-Commit Hook
Summarize staged changes and suggest doc updates automatically. You can turn this into a CI check or write directly to CHANGELOG.md.
Create .git/hooks/pre-commit (make it executable):
#!/usr/bin/env bash
set -euo pipefail
# Paste or source ai_chat here
ai_chat() { :; } # replace with the actual function
DIFF="$(git diff --cached)"
[ -z "$DIFF" ] && exit 0
read -r -d '' PROMPT <<'EOF' || true
You are a release-note and documentation assistant.
Given a unified diff of staged changes:
- Summarize notable changes for a human-readable CHANGELOG entry (concise bullets).
- Suggest docs sections (files/paths) to update with specific instructions.
- If CLI flags or config keys changed, list them verbatim.
Respond in Markdown with two sections: "Changelog" and "Docs Impact".
EOF
REPORT="$(ai_chat "$PROMPT
$DIFF
")"
mkdir -p .git/ai
printf "%s\n" "$REPORT" > .git/ai/docs-impact.md
echo "[ai] Wrote .git/ai/docs-impact.md (review before committing)."
# Optional: fail commit if docs impacted
if grep -qi 'docs impact' .git/ai/docs-impact.md; then
echo "[ai] Docs likely impacted. Please review .git/ai/docs-impact.md"
# exit 1 # uncomment to enforce discipline
fi
Review the generated .git/ai/docs-impact.md before committing, or enforce with exit 1 to block commits until docs are updated.
3) AI-Assisted man Pages from Markdown with pandoc
Formal docs help commands show up in man. This pattern keeps your voice consistent and converts to man pages automatically.
Draft or refine a CLI reference with AI.
Convert Markdown to a man page with pandoc.
Example script scripts/cli-man.sh:
#!/usr/bin/env bash
set -euo pipefail
# ai_chat function assumed available
SRC="${1:-docs/cli.md}"
OUT="${2:-build/cli.1}"
mkdir -p "$(dirname "$OUT")"
REFINED="$(ai_chat "Rewrite the following CLI reference for a UNIX man page style.
Constraints: short imperative sentences, clear OPTIONS, ENVIRONMENT, EXIT STATUS, EXAMPLES.
$(
[ -f "$SRC" ] && cat "$SRC" || echo "No docs/cli.md found."
)
")"
TMP="$(mktemp)"
trap 'rm -f "$TMP"' EXIT
printf "%s\n" "$REFINED" > "$TMP"
pandoc "$TMP" -s -t man -o "$OUT"
echo "Wrote man page: $OUT (try: man -l $OUT)"
Usage:
bash scripts/cli-man.sh docs/cli.md build/mytool.1
man -l build/mytool.1
4) Ask Questions About Your Repo with Context-Aware Q&A
A tiny RAG-lite flow: select relevant lines with ripgrep and hand them to the LLM. Great for onboarding and quick answers without building an index.
Create scripts/ask-docs.sh:
#!/usr/bin/env bash
set -euo pipefail
# ai_chat function assumed available
QUERY="${*:?Usage: ask-docs.sh <question>}"
CTX="$(rg --hidden -n --no-messages \
-g '!*.min.*' -g '!node_modules' -g '!.git' -g '!dist' -g '!build' -g '!vendor' \
-e "$QUERY" -e '^\s*#' -e '^## ' -e 'Usage: ' \
| head -n 400)"
read -r -d '' PROMPT <<'EOF' || true
Answer the user's question using ONLY the provided context from the repository.
- Quote file:line when citing specifics.
- If the answer is unclear or missing, say so and suggest where to add docs.
EOF
ai_chat "$PROMPT
Question: $QUERY
Context:
$CTX
"
Use it:
bash scripts/ask-docs.sh "How do I configure the API endpoint?"
Real-World Flow: Make Targets for Docs
Codify docs tasks so your team runs the same commands locally and in CI.
Makefile:
.PHONY: docs readme man ask
docs: readme man
readme:
bash scripts/ai-readme.sh
man:
bash scripts/cli-man.sh docs/cli.md build/mytool.1
ask:
bash scripts/ask-docs.sh "$(q)"
Then:
make readme
make man
make ask q="How do I pass auth tokens?"
Tips for Safe, Useful Outputs
Keep temperature low (0.0–0.3) for stable, factual docs.
Cap context size (head/trim) to avoid token overflows and cost spikes.
Check generated text into feature branches and review via PR like any code.
Pin models via $AI_MODEL in CI for reproducibility.
Never allow the AI to write to executable or critical files without review.
Conclusion and Next Steps
Bash is the glue that turns AI into a reliable teammate for documentation. Start small: 1) Install the essentials with apt/dnf/zypper. 2) Add ai_chat to your scripts folder. 3) Run scripts/ai-readme.sh to generate a first-pass README. 4) Wire the pre-commit hook to surface docs drift. 5) Convert your CLI reference into a real man page with pandoc.
From there, iterate: tune prompts, add CI checks, and standardize make targets across repos. Your docs will finally keep up with your code—without leaving the terminal.