🐚 Shell GPT

Natural language at the shell prompt — ask questions, get commands

aider curl fabric fzf gh Copilot jq ollama ripgrep sgpt tmux

What Is Shell GPT?

Shell GPT (command: sgpt) is a lightweight, open-source command-line tool that puts an AI assistant directly at your shell prompt. Unlike aider, which is focused on editing files, sgpt is conversational — you ask it questions, describe what you want to do, and it responds with answers, explanations, or ready-to-run shell commands.

For a Linux SysAdmin, sgpt fills the gap between "I know roughly what I need" and "I know the exact syntax." It handles one-liners, command flags, quick explanations, and shell pipeline construction — all without leaving the terminal or opening a browser.

sgpt supports OpenAI models out of the box, and can be configured to use other backends including local models via Ollama.

"sgpt is the closest thing to having a knowledgeable colleague standing next to you at the terminal — one who never gets tired of syntax questions."

💰 Cost & Licensing

sgpt itself is free and open source (MIT license). API costs depend on your backend:

Note: For typical SysAdmin use — a dozen queries a day — OpenAI API costs run well under $5/month. Heavy scripting sessions with large outputs will cost more.

🔧 Installation

sgpt is a Python package. The recommended install method is pipx to keep it isolated:

# Recommended: pipx (isolated environment) pipx install shell-gpt # Alternative: pip pip install shell-gpt # Verify sgpt --version

On first run, sgpt will prompt you for your OpenAI API key and store it in ~/.config/shell_gpt/.sgptrc.

Alternatively, set it as an environment variable:

export OPENAI_API_KEY=your-key-here

Using a local model with Ollama

If you want to run sgpt without sending data to OpenAI:

# Install Ollama first (see ollama.com) ollama pull llama3 # Configure sgpt to use Ollama sgpt --model ollama/llama3 "your query here"
Tip: Ollama local models are a great option for a lab or development server where you want AI assistance without any cloud dependency. For production SysAdmin work, cloud models are currently more capable.

💻 Basic Usage

Ask a plain question

sgpt "what does the -z flag do in tar?"

Get a shell command

Use --shell (or -s) to get a command ready to run:

sgpt -s "find all .log files under /var modified more than 30 days ago" # sgpt returns: find /var -name "*.log" -mtime +30

sgpt will ask if you want to execute, describe, or abort before running anything.

Execute directly

Add --execute to run the command immediately after confirmation:

sgpt -s --execute "show disk usage for each directory under /var, sorted by size"

Get a code snippet

sgpt --code "bash function to check if a process is running and restart it if not"

Pipe input into sgpt

One of sgpt's most powerful features — pipe command output directly into it:

# Explain what's in a log snippet tail -50 /var/log/messages | sgpt "summarize any errors in this log output" # Analyze df output df -h | sgpt "which filesystems are over 80% full?" # Check a config file cat /etc/httpd/conf/httpd.conf | sgpt "are there any obvious security issues in this Apache config?"

💬 Chat Mode — Persistent Conversations

By default each sgpt call is stateless. For multi-turn conversations where context matters, use --chat with a session name:

# Start a named chat session sgpt --chat debug-session "I'm seeing intermittent OOM kills on this server" # Continue the same session sgpt --chat debug-session "here's the output of free -h: ..." sgpt --chat debug-session "what should I check next in /proc/meminfo?"

sgpt remembers the full conversation within the named session, so follow-up questions have context. Sessions are stored locally in ~/.config/shell_gpt/chat_cache/.

Tip: Name your chat sessions descriptively — debug-apache, script-backup, plan-migration. They persist between terminal sessions so you can pick up where you left off.

🤖 Shell Integration — The CTRL+L Trick

sgpt can integrate with your shell so you can invoke it directly from the command line with a keyboard shortcut. After installing sgpt, add the shell integration to your .bashrc:

sgpt --install-integration

This adds a keybinding — typically Ctrl+L — that takes whatever you've typed at the prompt, sends it to sgpt as a shell command request, and replaces your typed text with the result. The workflow becomes:

  1. Type a plain English description at the prompt
  2. Press Ctrl+L
  3. sgpt replaces your text with the shell command
  4. Review it, press Enter to run
"Type what you mean. Let sgpt translate it into what the shell needs."
Important: Always review the generated command before pressing Enter. The shell integration makes it easy to run commands quickly — which is exactly why you should look before you leap, especially as root.

📝 Roles — Teaching sgpt Your Context

sgpt supports custom roles — system prompts that give the AI context about who you are and what kind of answers you want. Create a role file in ~/.config/shell_gpt/roles/:

# Create a SysAdmin role cat > ~/.config/shell_gpt/roles/sysadmin.json <<'EOF' { "name": "sysadmin", "role": "You are an experienced Linux/Unix system administrator with deep knowledge of RHEL, CentOS, and Debian-based systems. Provide concise, production-safe answers. Always mention potential risks for destructive commands. Prefer standard POSIX tools over GNU extensions when both work." } EOF

Use the role in any query:

sgpt --role sysadmin "what's the safest way to clear the systemd journal when disk is full?"

With a well-crafted role, sgpt's answers become more targeted and appropriately cautious for production environments.

✅ Strengths & Limitations

Works Well

  • Quick syntax and flag lookups
  • One-liner and pipeline construction
  • Piping command output for analysis
  • Log summarization and triage
  • Shell integration (Ctrl+L) workflow
  • Persistent chat sessions for debugging
  • Lightweight — minimal dependencies
  • Local model support for air-gapped use

Watch Out For

  • Primarily OpenAI-focused by default
  • No native file editing (use aider for that)
  • Shell integration needs careful review habits
  • Context window limits with large pipe inputs
  • Local models weaker for complex tasks
  • No awareness of your specific environment

📊 Craig's Take

sgpt occupies a different niche than Copilot or aider. It's not about editing files — it's about the moment-to-moment questions that come up when you're in the middle of something. "What's the rsync flag that preserves ACLs?" "Is there a one-liner to count unique IPs in an access log?" That's sgpt's territory.

The pipe integration is where it gets genuinely interesting for SysAdmins. Being able to pipe tail -f output or a config file into sgpt and ask "what's wrong here?" is a different kind of workflow than opening a browser and pasting text into Claude.ai. It keeps you in the terminal and in the flow of the work.

The shell integration (Ctrl+L) takes some getting used to — and some discipline, because it makes it fast to run commands you haven't fully reviewed. Once that habit is established, it's a genuinely faster way to work.

"sgpt is a man page that talks back — and actually understands what you're trying to accomplish."

🔗 Resources