Home AI SkillsShell-GPT: Generate and Explain Linux Commands with AI (sgpt)

Shell-GPT: Generate and Explain Linux Commands with AI (sgpt)

by Knowledge Base Linux
Shell-GPT generating Linux commands in a terminal illustration

Nobody remembers the exact find flags for “files modified in the last 3 days, bigger than 500 MB” — and googling it for the hundredth time is a waste of a sysadmin’s day. Shell-GPT (the sgpt command) puts an AI assistant directly in your terminal: describe what you want in plain English, get a shell command back, review it, and run it only if it looks right. This guide shows how to install Shell-GPT on Linux, use it safely, and make it part of your daily workflow.

What Shell-GPT Can Do

Shell-GPT has three personalities, switched by flags:

  • Answers: plain questions answered in the terminal (sgpt "what is a zombie process?").
  • Shell commands: with --shell, it generates a command and offers to execute it — after you confirm.
  • Code: with --code, it writes scripts and functions instead of prose.

It also supports chat sessions for follow-up questions and custom “roles” that change its behavior — for example a strict, safety-first sysadmin persona.

Prerequisites

  • A Linux machine with Python 3.10+ and pip.
  • An OpenAI API key, or a local Ollama endpoint if you prefer to keep prompts on your own hardware (see our Ollama guide).
  • A habit of reading commands before running them — non-negotiable with any AI tool.

Step 1: Install Shell-GPT

pip install shell-gpt
sgpt --version

On first run, Shell-GPT asks for your API key and stores its config in ~/.config/shell_gpt/. To supply the key via environment instead:

export OPENAI_API_KEY="sk-your-key-here"

To use a local model instead of the cloud, Shell-GPT supports alternative model names and base URLs through its configuration — point it at your Ollama server’s OpenAI-compatible endpoint (http://localhost:11434/v1) and a local model such as llama3.1:8b.

Step 2: Generate and Run Commands with Shell-GPT

The everyday workflow is one line:

sgpt --shell "find all files in /var larger than 500MB modified in the last 3 days"
# Output: find /var -type f -size +500M -mtime -3
# [E]xecute, [D]escribe, [A]bort:

The confirmation prompt is Shell-GPT’s most important feature. Press D to describe when you are unsure what a generated command does — it explains each flag before you decide. Press A whenever anything looks off. Typical useful requests:

sgpt --shell "show the 10 largest directories in /home"
sgpt --shell "list docker containers using more than 1GB of memory"
sgpt --shell "compress all .log files in /var/log/app older than 7 days"

Step 3: Explain Commands and Generate Code

Shell-GPT is just as valuable in reverse — understanding commands you inherited from a predecessor or found in documentation:

sgpt "explain this command: awk -F: '$3 >= 1000 {print $1}' /etc/passwd"
sgpt "what does systemctl mask do and when should I use it instead of disable?"

For scripts, switch to code mode and be specific about requirements:

sgpt --code "bash function that checks if a TCP port is open on a host with a 2 second timeout and prints OPEN or CLOSED"

Treat generated code like code from a forum: read it, test it in a safe environment, then keep it. The same review discipline applies to basics like the gzip command options it suggests.

Shell-GPT explaining a shell command step by step illustration

Step 4: Create a Safety-First Role and Use Chat Sessions

Roles are system prompts stored as files. Create one that enforces your house rules:

sgpt --create-role careful-admin

Describe the role as: “You are a cautious Linux sysadmin assistant. Prefer read-only commands. Never suggest force-deleting files, overwriting configs without backup, or disabling security tools. Prefer dry-run flags and explain destructive steps before giving them.” Then use it:

sgpt --role careful-admin --shell "clean up disk space used by old docker images"

Chat sessions keep context across questions, which is ideal while debugging:

sgpt --chat debug1 "nginx returns 502, what should I check first?"
# ...run the suggested checks, paste the output...
sgpt --chat debug1 "here is the error log output, what now?"

Pasting real output makes answers dramatically better — a pattern that also powers the workflows in our command-line guides and beyond.

Troubleshooting

sgpt: command not found after install. The pip scripts directory is not in your PATH. Add ~/.local/bin to PATH or reinstall with pipx install shell-gpt.

It generates commands for the wrong shell. Shell-GPT detects your shell, but in scripts or cron the context differs. State the shell in the request: “in bash, …”.

Answers are too creative. Use a strict role (Step 4) and lower the randomness in the config file (~/.config/shell_gpt/sgptrc) by reducing the temperature setting.

It keeps asking for an API key. Your environment variable is not set in the current shell, or the config file has an empty key. Export the variable in ~/.bashrc.

Conclusion

Shell-GPT removes the two worst frictions of terminal work: forgetting exact syntax and deciphering inherited commands. Its confirm-before-execute design makes it safe enough for production machines — provided you build the habit of pressing D before E. Combine it with a cautious custom role and a local model, and you have a private, tireless command-line mentor on every server you manage.

You may also like

KbLinux

Typically replies within a day

Hello, Welcome to the site. Please click below button for chatting me through Telegram.