# ShellGPT

> CLI that sends prompts to LLMs (OpenAI by default, Ollama supported) to generate shell commands, code, and answers, with stdin piping and chat sessions.

Stars: 12.3k · Language: Python · License: MIT · Categories: AI

- Repository: https://github.com/TheR1D/shell_gpt
- Page: https://www.trmn.sh/tools/shell-gpt

## Install

- pip: `pip install shell-gpt`

## Overview

ShellGPT (`sgpt`) is a command-line client for large language models. It takes a prompt from command-line arguments, stdin, or redirection operators (`<`, heredocs, `<<<`) and returns generated shell commands, code snippets, or plain answers. It uses OpenAI's API by default, with the key stored in `~/.config/shell_gpt/.sgptrc`. Local models can be run through a backend such as Ollama, though the README notes the tool is not optimized for local models. It supports Linux, macOS, and Windows, and works with shells including PowerShell, CMD, Bash, and Zsh.

The `--shell` (`-s`) option generates a command suited to your OS and `$SHELL`, then prompts to [E]xecute, [D]escribe, or [A]bort. For example, "update my system" yields a `softwareupdate` command on macOS and an `apt` command on Ubuntu. `--no-interaction` prints the command to stdout so it can be piped, for instance to `pbcopy`. The `--code` (`-c`) option returns code only, which can be redirected to a file. Piped input works with both modes, such as `git diff | sgpt "Generate git commit message, for my changes"` or `docker logs -n 20 my_app | sgpt "check logs, find errors, provide possible solutions"`.

Shell integration, installed with `sgpt --install-integration`, adds lines to `.bashrc` or `.zshrc`. Pressing `Ctrl+l` by default replaces the current input line with a suggested command, which you can edit before running. For conversation state, `--chat <name>` keeps a named session that can be combined with `--code` or `--shell` to refine output over several prompts. `--repl <name>` opens an interactive loop on the same session object, with multiline input using triple quotes. `--list-chats` and `--show-chat` list and inspect sessions. The tool suits developers and sysadmins who want to look up command syntax, draft snippets, or analyze logs and diffs without leaving the terminal.

### Installation

```bash
# via pip
pip install shell-gpt
```

![ShellGPT demo](https://media.trmn.sh/cached/8b110988203653f83331b838cb7157910e4368f2dd0b39261d160da8684e7901.mp4)

![ShellGPT shell integration](https://media.trmn.sh/cached/39445409a44a2fcdc249224760c3aa44df31382ada925c8c1864d63011c4d4a1.mp4)

![ShellGPT REPL mode](https://s10.gifyu.com/images/repl-demo.gif)

## Alternatives

- [llm](https://www.trmn.sh/tools/llm.md): CLI tool and Python library for interacting with OpenAI, Anthropic, Google, and dozens of other language models via APIs or locally (Python, 12.7k stars)
- [aider](https://www.trmn.sh/tools/aider.md): AI-powered terminal-based pair programming tool that integrates LLMs with your existing codebase and git workflow (Python, 49.8k stars)
- [Open Interpreter](https://www.trmn.sh/tools/open-interpreter.md): CLI tool that lets LLMs execute code locally through a ChatGPT-like terminal interface with approval prompts (Python, 68.7k stars)
- [AIChat](https://www.trmn.sh/tools/aichat.md): LLM CLI with a shell assistant, chat REPL, RAG, function calling and agents, supporting 20+ providers including OpenAI, Claude, Gemini and Ollama. (Rust, 10.5k stars)
- [Claude Squad](https://www.trmn.sh/tools/claude-squad.md): Terminal UI that runs multiple AI coding agents (Claude Code, Codex, Gemini, Aider) in parallel, each in its own tmux session and git worktree. (Go, 8.7k stars)

---

Canonical: https://www.trmn.sh/tools/shell-gpt
