- Posted on
- • Artificial Intelligence
Local Artificial Intelligence Models for Bash Automation
- Author
-
-
- User
- linuxbash
- Posts by this author
- Posts by this author
-
Local Artificial Intelligence Models for Bash Automation
If you could ask your terminal “draft a safe Bash script to batch-rename photos by EXIF date” and get a runnable, reviewed script in seconds—would you? Local AI models now make this a reality without sending your command history, logs, or secrets to the cloud.
In this guide, you’ll learn why local AI belongs in your Bash toolbox, how to install a runner, which models to pull, and 4 practical, safety-first patterns to automate your shell today.
Why local AI for Bash?
Privacy by default: Everything runs on your machine. No cloud API keys, no data exfiltration.
Offline and fast: 7B–8B parameter models are now practical on laptops and workstations.
Better fit for CLI: “Coder” and “Instruct” models are excellent at Bash, awk, sed, and jq patterns.
Composable: Talk to your model over a local HTTP API or CLI, pipe stdin, and keep everything scriptable.
Installation
We’ll set up the essentials, then choose between two popular local runners:
Ollama (easiest, batteries-included, model hub)
llama.cpp (build from source, very lightweight and flexible)
1) Prerequisites (curl, jq, ShellCheck, build tools)
Install with your package manager:
- Debian/Ubuntu (apt):
sudo apt update
sudo apt install -y curl jq shellcheck fzf git build-essential cmake
- Fedora/RHEL/CentOS (dnf):
sudo dnf install -y curl jq ShellCheck fzf git @development-tools cmake
- openSUSE (zypper):
sudo zypper refresh
sudo zypper install -y curl jq ShellCheck fzf git gcc make cmake
2A) Option A: Install Ollama (recommended to start)
curl -fsSL https://ollama.com/install.sh | sh
After install, verify the service:
ollama --version
systemctl --user status ollama 2>/dev/null || systemctl status ollama
Pull two solid starter models:
- A coder model for Bash-heavy tasks:
ollama pull qwen2.5-coder:7b
- A general instruct model:
ollama pull llama3.1:8b
You can switch models per task with an environment variable (shown below).
2B) Option B: Install llama.cpp (from source)
If you prefer a bare-metal runner, build llama.cpp:
Dependencies are already covered in “Prerequisites.”
Build:
git clone https://github.com/ggerganov/llama.cpp
cd llama.cpp
make -j
- Get a GGUF model (download a quantized .gguf file from a reputable source, e.g., a 7B coder model). Place it under models/, for example:
mkdir -p models
# Replace URL_TO_GGUF with the actual model URL you choose
curl -L -o models/qwen2.5-coder-7b.Q4_K_M.gguf "URL_TO_GGUF"
- Start the HTTP server on port 8080:
./llama-server -m models/qwen2.5-coder-7b.Q4_K_M.gguf --port 8080
Note: The examples below target Ollama’s API on 11434. If you use llama.cpp, adapt the URL and payload to its /completion API. The patterns remain the same.
Quick plumbing: a tiny ai() helper for your shell
With Ollama running locally, add this function to your ~/.bashrc or ~/.bash_profile:
# Ask your local model a question; optionally pipe input via stdin.
# Requires: ollama running on localhost:11434 and jq installed.
AI_MODEL="${AI_MODEL:-qwen2.5-coder:7b}"
ai() {
local prompt="$*"
local body
if [ -t 0 ]; then
# No stdin piped
body=$(jq -nc --arg m "$AI_MODEL" --arg p "$prompt" \
'{model:$m, prompt:$p, stream:false}')
else
# Read stdin and append to prompt
local input
input=$(cat)
body=$(jq -nc --arg m "$AI_MODEL" --arg p "$prompt" --arg i "$input" \
'{model:$m, prompt: ($p + "\n\nINPUT:\n" + $i), stream:false}')
fi
curl -fsS http://localhost:11434/api/generate \
-H 'Content-Type: application/json' \
-d "$body" | jq -r '.response'
}
- Set a different model any time:
export AI_MODEL=llama3.1:8b
4 high-impact ways to use local AI in Bash
1) Draft a script safely (generate → lint → confirm → run)
Let AI write the first draft, but keep you in control. Add these helpers:
ai-bash-gen() {
local spec="$*"
ai "Write a Bash script that does the following:\n$spec\n\
Constraints:\n\
- Output ONLY the script content (no markdown, no fencing, no explanations).\n\
- Use: set -euo pipefail.\n\
- Add helpful comments.\n\
- Avoid sudo and destructive commands.\n"
}
ai-safe-run() {
local spec="$*"
local tmp
tmp=$(mktemp)
ai-bash-gen "$spec" >"$tmp"
echo "Generated at: $tmp"
if ! shellcheck -s bash "$tmp"; then
echo "ShellCheck found issues. Fix or regenerate."
return 1
fi
if grep -Eqs '(rm -rf /|:>|mkfs|dd if=/dev/zero|shutdown|reboot|cryptsetup|chown -R /|mkfs\.)' "$tmp"; then
echo "Blocked: dangerous pattern detected in generated script."
sed -n '1,200p' "$tmp"
return 1
fi
echo "---- Preview (first 120 lines) ----"
sed -n '1,120p' "$tmp"
echo "-----------------------------------"
read -r -p "Run it now? [y/N] " ans
case "$ans" in
[yY]*) bash "$tmp" ;;
*) echo "Cancelled. Script saved at $tmp" ;;
esac
}
Real-world example:
ai-safe-run "Read images from ./photos, infer capture date from EXIF, \
then rename to YYYY-MM-DD_HH-MM-SS_originalname.jpg in ./renamed. \
Avoid overwriting and show a summary at the end."
Tip: iterate quickly—edit the draft in your editor, rerun ShellCheck, then execute.
Install ShellCheck if you skipped earlier:
apt: sudo apt install -y shellcheck
dnf: sudo dnf install -y ShellCheck
zypper: sudo zypper install -y ShellCheck
2) Explain and review one-liners before you trust them
Turn inscrutable incantations into step-by-step explanations:
explain() {
local cmd="$*"
ai "Explain this Bash command step by step, listing each pipe stage:\n$cmd"
}
explain 'grep -rI "password" /etc 2>/dev/null | awk -F: "{print \$1}" | sort -u'
This is great for code review, onboarding, and catching unintended side effects before you paste-and-pray.
3) Log triage and anomaly summaries
Pipe fresh logs through AI for a quick situational overview:
journalctl -u ssh -n 500 --no-pager \
| ai "Summarize failed logins, suspicious IPs, and noteworthy anomalies. \
Respond with concise bullet points and counts."
Or for Nginx:
tail -n 2000 /var/log/nginx/access.log \
| ai "Find unusual status codes, top offending IPs, and spikes. \
Return a short bulleted summary with counts."
4) Translate intent to jq/awk filters
Ask for just the filter, not prose—then test it yourself:
# Extract the jq filter only
jqfilter() {
ai "Given JSON with fields user, action, ts (ISO 8601), \
write ONLY a jq filter (no explanations) that outputs \
ts and user when action == \"login_failed\" and ts is today, \
sorted by ts ascending."
}
FILTER=$(jqfilter)
cat auth.json | jq "$FILTER"
Similarly for awk/sed:
awkfilter() {
ai "Write ONLY an awk program that reads 'user ip status' from stdin, \
and prints 'ip user' for lines where status == FAIL, deduplicated."
}
AWK=$(awkfilter)
cat ssh_attempts.txt | awk "$AWK"
Model and hardware notes
Start with 7B–8B “coder” or “instruct” models:
- qwen2.5-coder:7b (great for Bash and code)
- llama3.1:8b (general instruction following)
CPU-only works; GPU (6–8 GB VRAM) improves latency. Quantized models (e.g., Q4_K_M) balance speed and quality.
Keep prompts tight and specific; ask for “no explanations” if you only want code.
Security and safety checklist
Always lint generated scripts with ShellCheck.
Pre-flight syntax: bash -n script.sh
Manual preview before executing; require confirmation.
Block dangerous primitives in your wrapper (rm -rf /, mkfs, dd to devices, etc.).
Consider running risky scripts inside a container or sandbox (podman, docker, firejail).
Version-control accepted scripts; they become living documentation.
Troubleshooting
Ollama not responding:
- Check service: systemctl status ollama (or systemctl --user status ollama)
- Port check: ss -lntp | grep 11434
Slow responses:
- Try a smaller/quantized model, or switch to CPU/GPU that fits your workload.
Bad outputs:
- Be explicit in constraints (no explanations, POSIX only, avoid sudo, etc.).
- Provide a small example input and desired output format.
Your next step
Install the prerequisites and Ollama.
Paste the ai(), ai-bash-gen(), and ai-safe-run() helpers into your shell config.
Pull qwen2.5-coder:7b and llama3.1:8b.
Try one real task you do monthly—log triage, renaming, jq filters—and let AI draft the grunt work while you stay in charge.
Local AI won’t replace your Bash skills. It will amplify them—safely, privately, and right in your terminal.