- Posted on
- • Artificial Intelligence
Artificial Intelligence Terminal Tricks
- Author
-
-
- User
- linuxbash
- Posts by this author
- Posts by this author
-
Artificial Intelligence Terminal Tricks: Supercharge Your Bash Workflow
What if you could keep your hands on the keyboard, stay in the terminal, and still get AI-grade help explaining errors, drafting one-liners, and summarizing logs? No tab-switching. No copy/paste gymnastics. Just Bash.
In this post, you’ll learn practical, low-friction ways to blend AI into your existing shell workflow. You’ll get setup steps, copy-paste-ready functions, and real-world examples you can try today.
Why bring AI to the terminal?
Reduce context switching: Ask, generate, and iterate without leaving Bash.
Faster feedback loops: Explain errors, refine commands, and test immediately.
Private by default (if you want): Run models locally with Ollama—no cloud required.
Repeatable automation: Save prompts and helpers as shell functions, version them with your dotfiles.
Quick setup (Linux)
We’ll use small, well-supported tools and give you distro-specific install commands.
1) Install base tools
- Debian/Ubuntu (apt):
sudo apt update
sudo apt install -y pipx python3 python3-venv curl jq fzf
pipx ensurepath
- Fedora/RHEL/CentOS (dnf):
sudo dnf install -y pipx python3 python3-virtualenv curl jq fzf
pipx ensurepath
- openSUSE (zypper):
sudo zypper refresh
sudo zypper install -y pipx python3 python3-venv curl jq fzf
pipx ensurepath
Note: You may need to restart your shell or run source ~/.bashrc so pipx is on your PATH.
2) Choose your AI backend
Option A — Local (private) with Ollama:
curl -fsSL https://ollama.com/install.sh | sh
ollama pull llama3.2
Quick test:
echo "Explain what a process is in Linux" | ollama run llama3.2
Option B — Cloud (fast and simple) with Shell-GPT:
pipx install shell-gpt
export OPENAI_API_KEY="sk-...your key..."
sgpt "Write a bash one-liner that prints the current username and date."
Tip: Shell-GPT works with OpenAI-compatible APIs too. If you use an alternative provider with a custom base URL, export the appropriate env variable per your provider’s docs.
3) A tiny adapter: one function to talk to AI
Drop this into your ~/.bashrc (or ~/.bash_profile), then source it:
# USAGE:
# ai "your prompt"
# echo "some text" | ai
# Picks Ollama if installed; otherwise uses shell-gpt (sgpt).
ai() {
local model="${AI_MODEL:-llama3.2}" # default local model
if command -v ollama >/dev/null 2>&1; then
if [ -t 0 ]; then
printf "%s\n" "$*" | ollama run "$model"
else
cat | ollama run "$model"
fi
elif command -v sgpt >/dev/null 2>&1; then
if [ -t 0 ]; then
sgpt --model "${AI_CLOUD_MODEL:-gpt-4o-mini}" "$*"
else
# read all stdin then send to sgpt
local input
input="$(cat)"
sgpt --model "${AI_CLOUD_MODEL:-gpt-4o-mini}" "$input"
fi
else
echo "No AI backend found. Install ollama or shell-gpt (sgpt)." >&2
return 1
fi
}
Local default model can be changed:
export AI_MODEL="llama3.2"Cloud default model can be changed:
export AI_CLOUD_MODEL="gpt-4o-mini"
4 Terminal Tricks You’ll Actually Use
1) Explain the last error (and fix it)
Stop doomscrolling logs. Capture a command’s stderr and have AI suggest likely causes and fixes.
Add these helpers:
# Run commands through `try` to capture stderr.
try() {
: > /tmp/last-stderr.txt
"$@" 2> >(tee -a /tmp/last-stderr.txt >&2)
}
whyfail() {
if [ ! -s /tmp/last-stderr.txt ]; then
echo "No recent errors captured. Run your command with: try <command>" >&2
return 1
fi
{
echo "You are a Linux CLI assistant. Explain this error and propose specific fixes:"
echo
cat /tmp/last-stderr.txt
} | ai
}
Example:
try grep -R "needle" /root
whyfail
You’ll get a targeted explanation (e.g., permission issue) and concrete next steps (e.g., use sudo on specific paths, or adjust the pattern).
2) Generate safe, review-first one-liners
Let AI propose a Bash command but make it output-only so you can inspect before running.
ai_cmd() {
local task="$*"
ai "Return ONLY a safe, POSIX-compliant bash one-liner to: ${task}. \
No comments, no backticks, no explanations."
}
confirmrun() {
local cmd
cmd="$(ai_cmd "$*")" || return 1
echo "Candidate command:"
echo " $cmd"
read -r -p "Run it? [y/N] " ans
if [[ "$ans" =~ ^[Yy]$ ]]; then
echo "Executing..."
eval "$cmd"
else
echo "Aborted."
fi
}
Examples:
ai_cmd "list the 10 largest files under /var with sizes"
confirmrun "find and delete empty directories under ~/Downloads"
Pro tip: Pair this with bash -x to see exactly what runs:
cmd="$(ai_cmd "find all .log files under /var and show top 5 by size")"
bash -x -c "$cmd"
3) AI “tldr” for any command (grounded by real help/man)
Feed the tool’s own help to AI so suggestions stay accurate.
aitldr() {
local cmd="$1"
if [ -z "$cmd" ]; then
echo "Usage: aitldr <command>" >&2
return 1
fi
{
"$cmd" --help 2>&1 || true
echo
man "$cmd" 2>/dev/null | col -b || true
} | ai "From the following help/man text for '$cmd', produce 3–5 practical, copyable examples.
Each example: one command line then a brief 1-sentence description."
}
Example:
aitldr tar
aitldr find
You’ll get compact, context-aware examples that match the command actually installed on your system.
4) Summarize logs and diffs on the fly
Pipe noisy output to AI and ask for concise, actionable summaries.
- Summarize recent systemd errors:
journalctl -p err -n 200 | ai "Summarize the main error types, suspected root causes, and 3 concrete remediation steps."
- Explain a Git diff before code review:
git diff | ai "Summarize changes by file with bullet points. Call out risky operations, deletions, and config changes."
- Digest long command output:
df -h | ai "Identify filesystems at risk and actionable cleanup ideas."
Bonus: Use fzf to pick a log or file interactively, then summarize it:
summarize_pick() {
local file
file="$(fzf --prompt='Choose a file to summarize: ')" || return 1
<"$file" ai "Summarize this file. If it looks like logs, group errors and suggest fixes. If it's text, provide a bullet digest."
}
Real-world playbook
Package install failed? Wrap with
try, thenwhyfail.Unsure of the right
findincantation?ai_cmd "find files modified in last 24h larger than 50M under /home"and review it.On-call at 2 a.m.?
journalctl -u nginx --since '1 hour ago' | ai "Pinpoint the failures and quick fixes."New tool?
aitldr rsyncand copy a working example.
Uninstall/cleanup
- Shell-GPT:
pipx uninstall shell-gpt
- Ollama:
ollama rm llama3.2
sudo systemctl stop ollama
sudo rm -rf /usr/local/bin/ollama /usr/local/lib/ollama
(Adjust paths if installed elsewhere.)
Conclusion and next steps
You don’t need a new IDE or a heavy GUI to get AI-powered help—just a couple of tiny tools and a few shell functions.
Your next steps:
1) Install either Ollama (local) or Shell-GPT (cloud).
2) Paste the ai, try, whyfail, ai_cmd, and aitldr functions into your ~/.bashrc.
3) Run three experiments:
- try <a failing command> then whyfail
- ai_cmd "describe a task you do often" and review the output
- aitldr <a command you struggle with>
If you found these tricks useful, add them to your dotfiles and share your favorite prompts with your team. Happy hacking—without leaving the terminal.