Posted on
Artificial Intelligence

Artificial Intelligence Bash Productivity Hacks

Author
  • User
    linuxbash
    Posts by this author
    Posts by this author

Artificial Intelligence Bash Productivity Hacks: Turn Your Terminal into a Pair‑Programmer

Ever wasted 20 minutes hunting down a cryptic error, writing a gnarly awk one‑liner, or crafting a decent commit message? The slowest part of shell work isn’t your typing speed—it’s context switching, searching, and explaining. The fix: bring AI into your Bash workflow so you can stay in the terminal, move faster, and reduce mental overhead.

This guide shows you practical, low‑friction ways to weave AI into everyday Bash. You’ll get install commands, ready‑to‑paste functions, and real examples you can try right now.

Why AI belongs in your Bash toolkit

  • It speaks Bash fluently: AI is great at explaining errors, refactoring one‑liners, and turning plain language into commands—exactly what shell users need.

  • It fits the Unix philosophy: AI is just another text filter. You can pipe logs in, get summaries out, and compose it with jq, rg, fzf, git, and friends.

  • It reduces context switches: Keep your hands in the terminal while you ask for explanations, generate commands, or draft docs.

Note: Be mindful of secrets. Don’t send tokens, keys, or private data to cloud models. Redact, filter, or use local/on‑prem models where appropriate.


Install the toolkit

We’ll use a small set of battle‑tested tools:

  • curl, jq for APIs/JSON

  • fzf for fuzzy selection

  • ripgrep (rg) and fd for fast search

  • bash-completion for better tab completion

  • ShellCheck to lint shell scripts

  • tealdeer (command: tldr) for quick command examples

  • pipx to install the llm CLI (a flexible AI client you can point at various providers)

Install via your package manager.

Debian/Ubuntu (apt)

sudo apt update
sudo apt install -y curl jq fzf ripgrep fd-find bash-completion shellcheck tealdeer pipx python3-venv

On Debian/Ubuntu, fd is named fdfind. Add this to your shell to use fd:

if command -v fdfind >/dev/null && ! command -v fd >/dev/null; then alias fd=fdfind; fi

Initialize tldr pages:

tldr --update

Enable pipx on your PATH (restart shell after this):

pipx ensurepath

Fedora/RHEL (dnf)

sudo dnf install -y curl jq fzf ripgrep fd-find bash-completion ShellCheck tealdeer pipx python3-pip

Initialize tldr pages:

tldr --update

Enable pipx on your PATH (restart shell after this):

pipx ensurepath

openSUSE (zypper)

sudo zypper refresh
sudo zypper install -y curl jq fzf ripgrep fd bash-completion ShellCheck tealdeer python3-pipx

Initialize tldr pages:

tldr --update

Enable pipx on your PATH (restart shell after this):

pipx ensurepath

Install the AI CLI

We’ll use Simon Willison’s llm CLI (provider‑agnostic; supports multiple backends via plugins). Install with pipx:

pipx install llm

Add an API key for your chosen provider, for example OpenAI:

llm keys set openai

Pick a default model (substitute a model you have access to, e.g., gpt-4o-mini, gpt-4o, claude-3-5-sonnet, etc.):

echo 'export LLM_MODEL="gpt-4o-mini"' >> ~/.bashrc

Reload your shell:

exec "$SHELL" -l

Drop‑in Bash functions you’ll actually use

Paste the functions you want into ~/.bashrc and reload your shell. Each hack includes a real‑world example to try.

1) ai: Ask, explain, summarize—right in the pipe

A tiny wrapper that sends either your prompt or piped input (logs, errors, code) to the model and returns a concise answer.

ai() {
  command -v llm >/dev/null 2>&1 || { echo "llm CLI not found. Install with: pipx install llm" >&2; return 127; }
  local model="${LLM_MODEL:-gpt-4o-mini}"
  local instruction="$*"

  if [ -p /dev/stdin ]; then
    # Read piped input carefully (can be large)
    local input
    input="$(cat)"
    llm -m "$model" "You are a terse, helpful Bash/Unix assistant.
Task: $instruction

Input:
$input"
  else
    llm -m "$model" "$instruction"
  fi
}

Try it:

  • Explain a scary error
make 2>&1 | ai "Explain the error and list 3 likely fixes for Ubuntu"
  • Summarize recent service issues
journalctl -u ssh --since -2h | ai "Summarize recurring errors and propose actionable steps"
  • Translate a dense command
printf '%s\n' "tar -I 'zstd -T0' -cf backup.tar.zst /var/log" | ai "Explain each flag and the performance impact"

Tip: Don’t pipe secrets. If needed, redact:

some_command | sed -E 's/(password|token|api[-_ ]?key)=\S+/\1=REDACTED/Ig' | ai "Summarize safely"

2) ai-one: Plain English to safe one‑liner (with review)

Turn a description into a single command, preview it, and choose whether to run it.

ai-one() {
  command -v llm >/dev/null 2>&1 || { echo "llm CLI not found. Install with: pipx install llm" >&2; return 127; }
  local model="${LLM_MODEL:-gpt-4o-mini}"
  local spec="$*"
  [ -z "$spec" ] && { echo "Usage: ai-one <what you want to do>" >&2; return 2; }

  echo "# Spec: $spec" >&2
  # Ask for a single safe command—no explanations, so it's easy to review/execute.
  local cmd
  cmd="$(llm -m "$model" "Output ONLY a single safe Bash one-liner to do the following.

- Prefer read-only or preview flags where applicable.

- Avoid destructive actions unless explicitly requested.

- If 'fd' may not exist, prefer POSIX 'find'.

Task: $spec")"

  printf 'Candidate:\n%s\n' "$cmd" >&2
  read -r -p "Run it? [y/N] " reply
  if [[ "$reply" =~ ^[Yy]$ ]]; then
    eval "$cmd"
  else
    printf '%s\n' "$cmd"
  fi
}

Examples:

  • Generate a dry‑run cleanup:
ai-one "find and list empty directories under ./src but ignore .git"
  • Build a safe preview first, then explicitly ask for destructive:
ai-one "remove node_modules folders older than 30 days under ~/code, show what will be deleted first"

3) aih: Fuzzy‑pick a command from your history, then get an AI explanation

Use fzf to select a past command and get a step‑by‑step explanation with risks highlighted.

aih() {
  command -v fzf >/dev/null || { echo "fzf not found. Install via your package manager." >&2; return 127; }
  local cmd
  cmd="$(history | awk '{$1=""; sub(/^ +/, ""); print}' | tac | fzf --prompt='Select cmd to explain > ' --height=80% --border)"
  [ -z "$cmd" ] && return
  printf '%s\n' "$cmd" | ai "Explain this command line by line. Note any destructive flags and safer alternatives."
}

Try it:

aih

Pick a complex find/xargs/sed pipeline from your history. Get an immediate breakdown.


4) ai-commit: Turn diffs into excellent Conventional Commits

Generate a clear, concise commit message from staged changes.

ai-commit() {
  # Ensure there is something staged
  git diff --staged --quiet && { echo "Nothing staged." >&2; return 1; }
  git diff --staged | ai "Write a Conventional Commit message with:

- type (feat/fix/docs/chore/refactor/test/build/ci)

- 50-char subject

- short body with bullet points explaining why, not just what

- no backticks, no code blocks"
}

Try it:

git add -p
ai-commit

Paste the result into git commit or pipe it directly:

git commit -m "$(ai-commit)"

Always review before committing.


5) Shell script rescue: combine ShellCheck + AI suggestions

Let ShellCheck find issues and have AI propose a fixed version you can review.

ai-fix-sh() {
  local file="$1"
  [ -z "$file" ] && { echo "Usage: ai-fix-sh path/to/script.sh" >&2; return 2; }
  [ -f "$file" ] || { echo "No such file: $file" >&2; return 2; }

  local report
  report="$(shellcheck -f gcc "$file" || true)"
  printf '%s\n' "$report" | ai "Given the following ShellCheck diagnostics, produce an improved version of $file.
Constraints:

- POSIX-compatible where possible unless bashisms are required.

- Keep comments and behavior.

- Output ONLY the corrected file content, no extra commentary."
}

Try it:

ai-fix-sh ./backup.sh > backup.fixed.sh
diff -u ./backup.sh ./backup.fixed.sh | less

Review the diff, then adopt selectively.


Bonus: supercharge with tldr and fd/rg

Not AI, but synergistic with the above.

  • Explore commands with examples:
tldr tar
  • Fuzzy topics with previews:
fman() {
  command -v fzf >/dev/null || { echo "fzf not found." >&2; return 127; }
  local topic
  topic="$(compgen -c | sort -u | fzf --prompt='tldr> ' --preview 'tldr --color always {}' --preview-window=right:70%)"
  [ -n "$topic" ] && tldr "$topic"
}
  • Find context, then summarize with AI:
rg -n --hidden --glob '!.git' 'TODO|FIXME' | ai "Group by file and propose next actions. Keep it concise."

Practical guardrails

  • Redact: Pipe through sed/rg to strip secrets before ai.

  • Review: Use ai-one’s prompt/confirm flow; never run unknown output blindly.

  • Offline/Local: If you can’t send data to a cloud provider, point llm at a local backend via its plugins—or keep AI out of sensitive paths and only use it on public info.

  • Repro: Paste final commands into scripts or Makefiles so your workflow is transparent and repeatable.


Wrap‑up and next steps

AI doesn’t replace your shell skills—it amplifies them. Start small:

1) Install the toolkit above.
2) Add ai and ai-one to ~/.bashrc.
3) Try:

journalctl -u ssh --since -30m | ai "Summarize the top issues and likely fixes"

4) Try:

ai-one "list the 10 largest files under the current directory, human-readable, sorted"

If these two stick, layer on aih, ai-commit, and ai-fix-sh.

Got a favorite AI+Bash trick? Share it as a gist or dotfiles snippet so others can copy‑paste and go.