Posted on
Artificial Intelligence

Building an AI-Assisted Linux Workflow

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

Building an AI‑Assisted Linux Workflow

If you live in Bash, you already know the feeling: a sea of logs, cryptic errors, man pages longer than your lunch break, and the constant dance of remembering obscure flags. What if you could keep your hands on the keyboard and get targeted, context‑aware help on demand—without leaving the terminal? That’s what an AI‑assisted Linux workflow delivers.

This guide shows you how to wire AI into everyday shell tasks—explaining errors, drafting safe commands, writing commit messages, and summarizing logs—using small, auditable Bash helpers. You’ll get both local (no data leaves your machine) and cloud options, plus distro‑specific install steps.


Why this matters now

  • Models are fast and usable on consumer hardware. Tools like Ollama let you run capable LLMs locally.

  • HTTP + JSON makes models scriptable. With curl and jq you can build tiny, composable CLI helpers.

  • AI reduces cognitive load. Let models draft, summarize, and explain while you stay focused.


What you’ll build

  • A small ai helper that sends prompts from your terminal to either:

    • a local model (via Ollama), or
    • a cloud model (any OpenAI‑compatible provider).
  • 4 practical functions:

    • aix: explain a command or error
    • aic: turn a natural‑language intent into a safe Bash command
    • aicommit: generate a conventional commit from git diff
    • ailogs: summarize recent service logs

You can paste these into your ~/.bashrc or ~/.bash_aliases and extend as you like.


1) Install prerequisites

These tools are used for making HTTP requests, parsing JSON, and optional niceties like fuzzy finding.

  • Debian/Ubuntu (apt):
sudo apt update
sudo apt install -y curl jq git fzf ripgrep entr
  • Fedora/RHEL/CentOS (dnf):
sudo dnf install -y curl jq git fzf ripgrep entr
  • openSUSE (zypper):
sudo zypper refresh
sudo zypper install -y curl jq git fzf ripgrep entr

Notes:

  • curl and git may already be installed.

  • ripgrep provides rg; entr helps react to file changes (optional).

  • fzf is optional but handy for interactive flows.


2) Choose a model backend

Pick either local (privacy‑first) or cloud (strongest models) or configure both and let the script auto‑select.

A) Local: Ollama (recommended for on‑device use)

  • Install Ollama:
curl -fsSL https://ollama.com/install.sh | sh
  • Pull a model (pick one you trust and that fits your hardware):
ollama pull llama3.1
# or: ollama pull qwen2.5:7b
  • Verify:
curl -s http://localhost:11434/api/tags | jq
  • Optional env var (default is fine):
export OLLAMA_MODEL=llama3.1

B) Cloud: Any OpenAI‑compatible API

  • Set environment variables:
export AI_BASE_URL="https://api.openai.com/v1"   # or your provider’s base URL
export AI_API_KEY="sk-...your key..."
export AI_MODEL="gpt-4o-mini"                    # or another model your provider offers
  • Quick test (should return JSON):
curl -sS -H "Authorization: Bearer $AI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"model":"'"$AI_MODEL"'","messages":[{"role":"user","content":"Say hello"}]}' \
  "$AI_BASE_URL/chat/completions" | jq

Security tip: add API keys to a secrets file (~/.bash_secrets) and source it from ~/.bashrc with chmod 600 ~/.bash_secrets.


3) Add the ai helper

Create ~/.local/bin/ai (and ensure ~/.local/bin is in your PATH). This script reads from stdin or args, prefers cloud if AI_API_KEY is set, otherwise uses Ollama if available.

#!/usr/bin/env bash
set -euo pipefail

prompt="$*"
if [ -z "$prompt" ] && [ ! -t 0 ]; then
  prompt="$(cat)"
fi

if [ -z "${prompt:-}" ]; then
  echo "Usage: ai 'your question'  (or pipe input)" >&2
  exit 1
fi

# System instruction keeps answers concise and shell-focused
system_msg="You are a concise Linux assistant. Prefer POSIX/Bash. Show minimal, correct examples."

# Prefer cloud if API key is present
if [ -n "${AI_API_KEY:-}" ] && [ -n "${AI_BASE_URL:-}" ]; then
  model="${AI_MODEL:-gpt-4o-mini}"
  jq -n --arg m "$model" --arg sys "$system_msg" --arg p "$prompt" \
    '{model:$m, temperature:0.2,
      messages:[{role:"system",content:$sys},{role:"user",content:$p}]}' \
  | curl -sS -H "Authorization: Bearer $AI_API_KEY" \
           -H "Content-Type: application/json" \
           -d @- "$AI_BASE_URL/chat/completions" \
  | jq -r '.choices[0].message.content // empty'
  exit $?
fi

# Fallback to local Ollama
ollama_host="${OLLAMA_HOST:-http://localhost:11434}"
model="${OLLAMA_MODEL:-llama3.1}"

jq -n --arg m "$model" --arg p "$prompt" \
  '{model:$m, prompt:$p, stream:false}' \
| curl -sS -H "Content-Type: application/json" \
         -d @- "$ollama_host/api/generate" \
| jq -r '.response // empty'

Make it executable:

chmod +x ~/.local/bin/ai

Test it:

echo "How do I recursively count lines of code, excluding node_modules?" | ai

4) Drop‑in helpers you’ll actually use

Add these to ~/.bashrc or ~/.bash_aliases, then source the file.

  • Explain a command or error: aix
aix() {
  if [ $# -eq 0 ] && [ ! -t 0 ]; then
    ai "Explain this shell error/output and suggest a fix:\n\n$(cat)"
  else
    ai "Explain what this command does, common pitfalls, and safer alternatives:\n\n$*"
  fi
}

Examples:

aix 'find . -type f -mtime -1 -print0 | xargs -0 grep -n "TODO"'
dmesg --level=err | tail -100 | aix
  • Draft a safe command from intent: aic
aic() {
  ai "You are a Bash expert. Return ONLY a single safe command (no prose) for:\n$*"
}

Examples:

aic "kill all processes listening on TCP port 8000"
aic "convert all .png in images/ to webp at quality 80 into images/webp/"
  • Generate a conventional commit message: aicommit
aicommit() {
  local diff
  diff="$(git diff --staged)"
  if [ -z "$diff" ]; then
    echo "No staged changes." >&2; return 1
  fi
  ai "Write a concise Conventional Commit message for this diff.
Rules:

- Use one of: feat, fix, docs, chore, refactor, test, perf, build, ci

- Subject <= 72 chars, no period

- Include short body/bullets if helpful
Diff:
$diff"
}

Usage:

git add -A
aicommit
# If happy:
aicommit | head -n1 | git commit -F -
  • Summarize recent service logs: ailogs
ailogs() {
  local unit="${1:-ssh}"
  local since="${2:--1h}"
  journalctl -u "$unit" --no-pager -S "$since" \
  | ai "Summarize recurring errors/warnings with counts, likely causes, and 2 concrete remediation steps. Be concise."
}

Examples:

ailogs nginx -2h
ailogs docker -30m

Optional: bind a quick key in Bash for aix or add fzf flows later.


5) Real‑world examples

  • You have a failing pipeline step:
cat ci.log | tail -200 | aix
  • Need a one‑liner to remove orphaned pyc files:
aic "safely delete all .pyc not tracked by git"
  • Triage a noisy service:
ailogs NetworkManager -1h
  • Explain a gnarly sed:
aix 'sed -E "s/^([[:space:]]+)|([[:space:]]+)$//g" file.txt'

Safety, privacy, and cost control

  • Never auto‑run AI output. Inspect first. If you must, use a review step:
cmd="$(aic "find and gzip logs older than 7 days in /var/log/myapp")"
printf "About to run:\n%s\nProceed? [y/N] " "$cmd"; read -r ans
[ "$ans" = "y" ] && eval "$cmd"
  • Redact secrets when sending logs to cloud APIs:
journalctl -u myapp \
 | sed -E 's/(password|token|apikey)=\S+/\1=REDACTED/gi' \
 | ai "Summarize issues and fixes"
  • Prefer local models for sensitive data (Ollama).

  • Use smaller/cheaper models for routine prompts:

export AI_MODEL="gpt-4o-mini"
  • Keep temperature low (0.2) for predictable answers.

Troubleshooting

  • ai: command not found
    • Ensure ~/.local/bin is in PATH:
echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc
  • Ollama errors

    • Check the service: pgrep -x ollama || ollama serve &
    • Verify endpoint: curl -s http://localhost:11434/api/tags | jq
  • Cloud 401/404

    • Recheck AI_BASE_URL, AI_API_KEY, AI_MODEL and provider docs.
    • Confirm your model name is available to your account.

Conclusion and next steps

You now have a keyboard‑first AI copilot that lives where you do—inside Bash. Start small: use aix to explain outputs and aic to draft commands. As trust builds, wire in aicommit and ailogs, and tweak prompts for your stack.

Next steps:

  • Add shell completions or keybindings to trigger aix on the last command’s stderr.

  • Create project‑specific system prompts (e.g., “assume Debian, systemd, nginx”).

  • Explore local models that fit your GPU/CPU budget and tune for speed.

Your terminal just got smarter—without getting in your way.