- Posted on
- • Artificial Intelligence
Artificial Intelligence Bash Tips That Save Hours Every Week
- Author
-
-
- User
- linuxbash
- Posts by this author
- Posts by this author
-
Artificial Intelligence Bash Tips That Save Hours Every Week
If you spend your day in a terminal, you already know the pain: breaking your flow to search for flags, craft gnarly awk/sed one‑liners, or write commit messages. What if you could stay in Bash, describe your intent in plain English, and get a safe, reviewable command or explanation instantly?
This article shows exactly that—practical, shell-first ways to use AI that will save you hours every week. You’ll set up lightweight CLI tools, wire them into small Bash helpers, and use them for everyday tasks while keeping safety and control.
What you’ll get:
Fast “English → command” with confirmation before execution
Clear “explain this command” breakdowns
Instant Git commit messages from diffs
Speed-ups for data wrangling and docs lookup—all from Bash
Why using AI in your shell actually works
Shell tasks are pattern-heavy and repetitive. LLMs excel at turning patterns and intent into commands and text.
The shell is glue. AI can draft commands that you review and run—without leaving your terminal.
You stay in control. With dry-runs, confirmations, and small helper functions, AI suggests while you decide.
Installation: minimal, cross-distro setup
We’ll install three things:
1) Prerequisites: curl, jq, fzf, pipx
2) An AI CLI path:
- Local models: Ollama (no cloud needed), or
- Cloud models: “llm” (model-agnostic CLI via pipx)
3) A few Bash functions you can paste into ~/.bashrc or ~/.bash_functions
1) Prerequisites
- Debian/Ubuntu (apt)
sudo apt update
sudo apt install -y curl jq fzf pipx
pipx ensurepath
- Fedora/RHEL (dnf)
sudo dnf install -y curl jq fzf pipx
pipx ensurepath
- openSUSE (zypper)
sudo zypper install -y curl jq fzf pipx
pipx ensurepath
2A) Local models with Ollama (private, offline)
Ollama runs LLMs on your machine.
- Install:
curl -fsSL https://ollama.com/install.sh | sh
- Pull a model (example: Llama 3.1)
ollama pull llama3.1
- Quick test:
ollama run llama3.1 "Write a one-line shell tip."
2B) Cloud models with “llm” (model-agnostic CLI)
Install the CLI:
pipx install llm
Configure a provider (choose one):
- OpenAI:
llm keys set openai
# Paste your key when prompted
- OpenRouter (multi-model gateway):
llm keys set openrouter
# Paste your key when prompted
Test it:
llm -m gpt-4o-mini "Write a one-line shell tip."
Tip: You can set your preferred model once:
export LLM_MODEL="gpt-4o-mini"
Add that to your ~/.bashrc to persist.
Core tips: 5 shell-first AI helpers you’ll actually use
Paste any of the following helpers into your ~/.bashrc, then source ~/.bashrc. Each helper prefers llm if installed; otherwise it will try ollama. They’re designed for safety-first usage.
Note: Replace default models via environment variables:
For llm:
export LLM_MODEL="gpt-4o-mini"For Ollama:
export OLLAMA_MODEL="llama3.1"
1) Turn plain English into a safe, reviewable command
This prints a single best-guess command. You decide what to do next.
ai() {
local prompt="As a Bash assistant, respond with exactly one POSIX-compliant shell command on a single line, no backticks, no explanations. Task: $*"
if command -v llm >/dev/null 2>&1; then
llm -m "${LLM_MODEL:-gpt-4o-mini}" "$prompt"
elif command -v ollama >/dev/null 2>&1; then
ollama run "${OLLAMA_MODEL:-llama3.1}" "$prompt"
else
echo "Install 'llm' (pipx) or 'ollama' first." >&2
return 1
fi
}
Want a confirm-and-run flow? Use ai-do:
ai-do() {
local cmd
cmd="$(ai "$@")" || return
printf 'AI suggests:\n %s\nRun it? [y/N] ' "$cmd" >&2
read -r ans
if [ "$ans" = "y" ] || [ "$ans" = "Y" ]; then
eval "$cmd"
fi
}
Real-world examples:
- Preview large files in your repo:
ai "show the 10 largest files tracked by git, human sizes"
- Dry-run cleanup in Downloads:
ai "find and print empty directories under ~/Downloads older than 7 days (dry-run)"
If it looks good, refine or run:
ai-do "compress all *.log in /var/log into a timestamped tar.gz in /tmp"
Safety tip: Start with read-only or print-only operations (-print, -ls, --dry-run) and only then confirm a destructive version.
2) Explain any command like a teammate would
explain() {
local cmd="$*"
local prompt="Explain this shell command step by step. Call out any destructive flags and safer alternatives. Command: $cmd"
if command -v llm >/dev/null 2>&1; then
llm -m "${LLM_MODEL:-gpt-4o-mini}" "$prompt"
elif command -v ollama >/dev/null 2>&1; then
ollama run "${OLLAMA_MODEL:-llama3.1}" "$prompt"
else
echo "Install 'llm' or 'ollama' first." >&2
return 1
fi
}
Example:
explain 'rsync -avz --delete src/ user@host:/srv/app/'
Great for onboarding, code reviews, and sanity checks before running risky commands.
3) AI-powered Git commit messages from staged changes
Let the AI propose a Conventional Commits-style message using your staged diff.
aicomm() {
if ! git rev-parse --git-dir >/dev/null 2>&1; then
echo "Not a git repo." >&2
return 1
fi
if [ -z "$(git diff --staged)" ]; then
echo "No staged changes." >&2
return 1
fi
local diff
diff="$(git diff --staged)"
local prompt="Write a concise Conventional Commits message (type: scope): summary
Rules:
- Use present tense, imperative mood
- One-line summary <= 72 chars
- Then a blank line, then bullet points of key changes
- Reference files or functions when useful
Diff:
$diff"
if command -v llm >/dev/null 2>&1; then
llm -m "${LLM_MODEL:-gpt-4o-mini}" "$prompt"
else
ollama run "${OLLAMA_MODEL:-llama3.1}" "$prompt"
fi
}
Usage:
git add .
aicomm | tee /tmp/COMMIT_MSG
git commit -F /tmp/COMMIT_MSG
4) Let AI draft awk/sed/jq one-liners (you review)
ai-code() {
local prompt="Write a single POSIX shell one-liner using awk|sed|jq for the task. Output only the command, no backticks or explanation. Task: $*"
if command -v llm >/dev/null 2>&1; then
llm -m "${LLM_MODEL:-gpt-4o-mini}" "$prompt"
else
ollama run "${OLLAMA_MODEL:-llama3.1}" "$prompt"
fi
}
Examples:
- Convert Nginx access log to CSV of ip,method,path,status:
ai-code "parse /var/log/nginx/access.log and output CSV: ip,method,path,status"
- Extract error lines from JSON logs using jq:
ai-code "from app.jsonl print timestamp and message where level==\"error\" with jq"
Tip: Install jq if you haven’t already:
- apt:
sudo apt install -y jq
- dnf:
sudo dnf install -y jq
- zypper:
sudo zypper install -y jq
Always eyeball the output before running, especially for sed -i or destructive filters.
5) Summarize man pages and long docs right in the shell
Turn dense pages into focused, example-rich summaries.
ai-man() {
if [ -z "$1" ]; then
echo "Usage: ai-man <command>" >&2
return 1
fi
local page
# Render man to plain text
page="$(man "$1" | col -b)"
local prompt="Summarize the most common, practical uses of '$1' with 3–5 example commands. Highlight risky flags. Man page content follows:\n\n${page}"
if command -v llm >/dev/null 2>&1; then
printf "%s" "$prompt" | llm -m "${LLM_MODEL:-gpt-4o-mini}"
else
printf "%s" "$prompt" | ollama run "${OLLAMA_MODEL:-llama3.1}"
fi
}
Example:
ai-man tar
Pro tips for safe and effective use
Start non-destructive: Ask for preview-style commands first, then iterate.
Confirm before eval: Use
ai-doso you always eyeball the command.Keep prompts tight: Tell the AI “single command, no explanations” to reduce noise.
Prefer absolute paths and quotes: Safer in scripts, fewer surprises.
Version your helpers: Keep these functions in a dotfiles repo for easy portability.
Your next step
Install the prerequisites and one AI path (local Ollama or cloud “llm”).
Paste the helpers into
~/.bashrc.Try these three mini-missions: 1)
ai "list the 15 largest files under ~/Projects with sizes"2)explain 'find . -type f -mtime -1 -print0 | xargs -0 -I{} cp {} /backup/'3)git add -A && aicomm
If this sped you up, share it with your team and drop your best “English → command” wins in your README. The more you keep AI in the flow of Bash, the more time you get back.