AI Agent Hub
AI エージェント / プラグイン情報メディア
← 一覧へ
Shell GPT
CODEX

Shell GPT

Shell GPT

ターミナル内で OpenAI などの LLM と対話しながらシェルコマンドや Git 操作を生成・実行する CLI。Function calling と REPL モードに対応する老舗ツール。

原文: A command-line productivity tool powered by AI large language models like GPT-5, will help you accomplish your tasks faster and more efficiently.
#CLI#shell#function-calling#chatgpt#cheat-sheet#cli#commands#gpt-3#gpt-4#gpt-5#linux#llama
REPO STATS

リポジトリ統計

⭐ Stars
12k
🍴 Forks
960
⚠️ Open Issues
115
🌿 Language
Python
📄 License
MIT
🕒 最終更新
2026.05.06 (1日前)
📅 公開日
2023.01.19
🌿 Branch
main
README

ドキュメント

— AI による自動翻訳 (2026.05.07 更新)

ShellGPT

AI大言語モデル(LLM)搭動く、コマンドラインの生産性向上ツールです。このコマンドラインツールは、シェルコマンド、コードスニペット、ドキュメントの効率的な生成を提供し、外部リソース(Google検索など)の必要性を排除します。Linux、macOS、Windows に対応しており、PowerShell、CMD、Bash、Zsh などすべての主要なシェルと互換性があります。

main_h264_high_bitrate.mov

インストール

pip install shell-gpt

デフォルトでは、ShellGPT は OpenAI の API と GPT-4 モデルを使用します。API キーが必要です。ここで生成できます。キーの入力を求められ、その後 ~/.config/shell_gpt/.sgptrc に保存されます。OpenAI API は有料サービスです。詳しくは OpenAI の価格情報を参照してください。

ヒント

または、オープンソースモデルをローカルで無料で実行することができます。これには Ollama などのような独自の LLM バックエンドの設定が必要です。ShellGPT を Ollama で動作させるには、この詳細なガイドに従ってください。

❗️ShellGPT はローカルモデルに対して最適化されていないため、期待通りに動作しない場合があります。

使用方法

ShellGPT は、情報を素早く分析・取得するために設計されています。技術設定から一般知識まで、さまざまなシンプルなリクエストに対応できます。

sgpt "What is the fibonacci sequence"
# -> The Fibonacci sequence is a series of numbers where each number ...

ShellGPT はプロンプトを標準入力とコマンドライン引数の両方から受け入れます。ターミナルを通じて入力をパイプするか、直接引数として指定するか、どちらを選んでも sgpt に対応できます。例えば、diff に基づいて Git のコミットメッセージを簡単に生成できます:

git diff | sgpt "Generate git commit message, for my changes"
# -> Added main feature details into README.md

プロンプトと一緒に標準入力を使用して、さまざまなソースからログを分析できます。例えば、ログを素早く分析し、エラーを特定して、考えられる解決策の提案を得ることができます:

docker logs -n 20 my_app | sgpt "check logs, find errors, provide possible solutions"
Error Detected: Connection timeout at line 7.
Possible Solution: Check network connectivity and firewall settings.
Error Detected: Memory allocation failed at line 12.
Possible Solution: Consider increasing memory allocation or optimizing application memory usage.

入力を渡すために、あらゆる種類のリダイレクト演算子も使用できます:

sgpt "summarise" < document.txt
# -> The document discusses the impact...
sgpt << EOF
What is the best way to lear Golang?
Provide simple hello world example.
EOF
# -> The best way to learn Golang...
sgpt <<< "What is the best way to learn shell redirects?"
# -> The best way to learn shell redirects is through...

シェルコマンド

find などの一般的なシェルコマンドを忘れてしまい、オンラインで構文を調べる必要があったことがありませんか?--shell オプション、またはショートカット -s を使用すれば、ターミナル内で必要なコマンドをすぐに生成・実行できます。

sgpt --shell "find all json files in current folder"
# -> find . -type f -name "*.json"
# -> [E]xecute, [D]escribe, [A]bort: e

Shell GPT はあなたが使用している OS と $SHELL を認識し、あなたのシステム固有のシェルコマンドを提供します。例えば、sgpt にシステムを更新するよう依頼すると、あなたの OS に基づいたコマンドが返されます。macOS を使用した例を以下に示します:

sgpt -s "update my system"
# -> sudo softwareupdate -i -a
# -> [E]xecute, [D]escribe, [A]bort: e

同じプロンプトを Ubuntu で使用すると、異なる提案が生成されます:

sgpt -s "update my system"
# -> sudo apt update && sudo apt upgrade -y
# -> [E]xecute, [D]escribe, [A]bort: e

Docker で試してみましょう:

sgpt -s "start nginx container, mount ./index.html"
# -> docker run -d -p 80:80 -v $(pwd)/index.html:/usr/share/nginx/html/index.html nginx
# -> [E]xecute, [D]escribe, [A]bort: e

パイプを使用して入力を sgpt に渡し、シェルコマンドを生成することもできます:

sgpt -s "POST localhost with" < data.json
# -> curl -X POST -H "Content-Type: application/json" -d '{"a": 1, "b": 2}' http://localhost
# -> [E]xecute, [D]escribe, [A]bort: e

プロンプトに追加のシェルマジックを適用し、この例では ffmpeg にファイル名を渡しています:

ls
# -> 1.mp4 2.mp4 3.mp4
sgpt -s "ffmpeg combine $(ls -m) into one video file without audio."
# -> ffmpeg -i 1.mp4 -i 2.mp4 -i 3.mp4 -filter_complex "[0:v] [1:v] [2:v] concat=n=3:v=1 [v]" -map "[v]" out.mp4
# -> [E]xecute, [D]escribe, [A]bort: e

生成されたシェルコマンドをパイプを使用して渡したい場合は、--no-interaction オプションを使用できます。これはインタラクティブモードを無効にし、生成されたコマンドを stdout に出力します。この例では pbcopy を使用して、生成されたコマンドをクリップボードにコピーしています:

sgpt -s "find all json files in current folder" --no-interaction | pbcopy

シェル統合

これは 非常に便利な機能 で、sgpt とプロンプト・引数を入力する必要なく、ターミナル内で sgpt シェル補完を直接使用できます。シェル統合により、Bash と ZSH シェルの両方でサポートされているホットキーを使用してターミナルで ShellGPT を使用できるようになります。この機能は sgpt 補完をターミナルバッファ(入力行)に直接挿入し、提案されたコマンドを即座に編集できるようにします。

Shell_GPT_Integration.mp4

シェル統合をインストールするには、sgpt --install-integration を実行し、ターミナルを再起動して変更を反映してください。これにより、.bashrc または .zshrc ファイルに数行が追加されます。その後、Ctrl+l(デフォルト)を使用して ShellGPT を呼び出すことができます。Ctrl+l を押すと、現在の入力行(バッファ)が提案されたコマンドに置き換わります。その後、それを編集して Enter を押して実行できます。

コード生成

--code または -c パラメータを使用することで、純粋なコード出力を特別にリクエストできます。例えば以下のようにします:

sgpt --code "solve fizz buzz problem using python"
for i in range(1, 101):
    if i % 3 == 0 and i % 5 == 0:
        print("FizzBuzz")
    elif i % 3 == 0:
        print("Fizz")
    elif i % 5 == 0:
        print("Buzz")
    else:
        print(i)

有効な Python コードなので、出力をファイルにリダイレクトできます:

sgpt --code "solve classic fizz buzz problem using Python" > fizz_buzz.py
python fizz_buzz.py
# 1
# 2
# Fizz
# 4
# Buzz
# ...

また、パイプを使用して入力を渡すこともできます:

cat fizz_buzz.py | sgpt --code "Generate comments for each line of my code"
# Loop through numbers 1 to 100
for i in range(1, 101):
    # Check if number is divisible by both 3 and 5
    if i % 3 == 0 and i % 5 == 0:
        # Print "FizzBuzz" if number is divisible by both 3 and 5
        print("FizzBuzz")
    # Check if number is divisible by 3
    elif i % 3 == 0:
        # Print "Fizz" if number is divisible by 3
        print("Fizz")
    # Check if number is divisible by 5
    elif i % 5 == 0:
        # Print "Buzz" if number is divisible by 5
        print("Buzz")
    # If number is not divisible by 3 or 5, print the number itself
    else:
        print(i)

チャットモード

会話を保持し再呼び出しすることは重要です。sgpt は、LLM 完了要求ごとに会話型ダイアログを作成します。ダイアログは 1 つずつ (チャットモード) または対話的に REPL ループ (REPL モード) で展開されます。両方の方法は同じ基礎オブジェクト (chat session と呼ばれます) に依存しています。セッションは 設定可能な CHAT_CACHE_PATH に格納されています。

会話を開始するには、--chat オプションに続いて一意のセッション名とプロンプトを使用します。

sgpt --chat conversation_1 "please remember my favorite number: 4"
# -> I will remember that your favorite number is 4.
sgpt --chat conversation_1 "what would be my favorite number + 4?"
# -> Your favorite number is 4, so if we add 4 to it, the result would be 8.

チャットセッションを使用して、追加の詳細情報を提供することで GPT の提案を段階的に改善できます。--code または --shell オプションを使用して --chat を開始することが可能です。

sgpt --chat conversation_2 --code "make a request to localhost using python"
import requests

response = requests.get('http://localhost')
print(response.text)

LLM にリクエストにキャッシングを追加するよう要求しましょう。

sgpt --chat conversation_2 --code "add caching"
import requests
from cachecontrol import CacheControl

sess = requests.session()
cached_sess = CacheControl(sess)

response = cached_sess.get('http://localhost')
print(response.text)

シェルコマンドにも同じことが当てはまります。

sgpt --chat conversation_3 --shell "what is in current folder"
# -> ls
sgpt --chat conversation_3 "Sort by name"
# -> ls | sort
sgpt --chat conversation_3 "Concatenate them using FFMPEG"
# -> ffmpeg -i "concat:$(ls | sort | tr '\n' '|')" -codec copy output.mp4
sgpt --chat conversation_3 "Convert the resulting file into an MP3"
# -> ffmpeg -i output.mp4 -vn -acodec libmp3lame -ac 2 -ab 160k -ar 48000 final_output.mp3

いずれかの会話モードからすべてのセッションをリスト表示するには、--list-chats または -lc オプションを使用します。

sgpt --list-chats
# .../shell_gpt/chat_cache/conversation_1  
# .../shell_gpt/chat_cache/conversation_2

特定の会話に関連するすべてのメッセージを表示するには、--show-chat オプションに続いてセッション名を使用します。

sgpt --show-chat conversation_1
# user: please remember my favorite number: 4
# assistant: I will remember that your favorite number is 4.
# user: what would be my favorite number + 4?
# assistant: Your favorite number is 4, so if we add 4 to it, the result would be 8.

REPL モード

— GitHub から取得した原文(一部省略の場合あり)

A command-line productivity tool powered by AI large language models (LLM). This command-line tool offers streamlined generation of shell commands, code snippets, documentation, eliminating the need for external resources (like Google search). Supports Linux, macOS, Windows and compatible with all major Shells like PowerShell, CMD, Bash, Zsh, etc.

main_h264_high_bitrate.mov

Installation

pip install shell-gpt

By default, ShellGPT uses OpenAI's API and GPT-4 model. You'll need an API key, you can generate one here. You will be prompted for your key which will then be stored in ~/.config/shell_gpt/.sgptrc. OpenAI API is not free of charge, please refer to the OpenAI pricing for more information.

Tip

Alternatively, you can run open-source models locally for free. This requires setting up your own LLM backend, such as Ollama. To get ShellGPT working with Ollama, follow this detailed guide

❗️Note that ShellGPT is not optimized for local models and may not work as expected.

Usage

ShellGPT is designed to quickly analyse and retrieve information. It's useful for straightforward requests ranging from technical configurations to general knowledge.

sgpt "What is the fibonacci sequence"
# -> The Fibonacci sequence is a series of numbers where each number ...

ShellGPT accepts prompt from both stdin and command line argument. Whether you prefer piping input through the terminal or specifying it directly as arguments, sgpt got you covered. For example, you can easily generate a git commit message based on a diff:

git diff | sgpt "Generate git commit message, for my changes"
# -> Added main feature details into README.md

You can analyze logs from various sources by passing them using stdin, along with a prompt. For instance, we can use it to quickly analyze logs, identify errors and get suggestions for possible solutions:

docker logs -n 20 my_app | sgpt "check logs, find errors, provide possible solutions"
Error Detected: Connection timeout at line 7.
Possible Solution: Check network connectivity and firewall settings.
Error Detected: Memory allocation failed at line 12.
Possible Solution: Consider increasing memory allocation or optimizing application memory usage.

You can also use all kind of redirection operators to pass input:

sgpt "summarise" < document.txt
# -> The document discusses the impact...
sgpt << EOF
What is the best way to lear Golang?
Provide simple hello world example.
EOF
# -> The best way to learn Golang...
sgpt <<< "What is the best way to learn shell redirects?"
# -> The best way to learn shell redirects is through...

Shell commands

Have you ever found yourself forgetting common shell commands, such as find, and needing to look up the syntax online? With --shell or shortcut -s option, you can quickly generate and execute the commands you need right in the terminal.

sgpt --shell "find all json files in current folder"
# -> find . -type f -name "*.json"
# -> [E]xecute, [D]escribe, [A]bort: e

Shell GPT is aware of OS and $SHELL you are using, it will provide shell command for specific system you have. For instance, if you ask sgpt to update your system, it will return a command based on your OS. Here's an example using macOS:

sgpt -s "update my system"
# -> sudo softwareupdate -i -a
# -> [E]xecute, [D]escribe, [A]bort: e

The same prompt, when used on Ubuntu, will generate a different suggestion:

sgpt -s "update my system"
# -> sudo apt update && sudo apt upgrade -y
# -> [E]xecute, [D]escribe, [A]bort: e

Let's try it with Docker:

sgpt -s "start nginx container, mount ./index.html"
# -> docker run -d -p 80:80 -v $(pwd)/index.html:/usr/share/nginx/html/index.html nginx
# -> [E]xecute, [D]escribe, [A]bort: e

We can still use pipes to pass input to sgpt and generate shell commands:

sgpt -s "POST localhost with" < data.json
# -> curl -X POST -H "Content-Type: application/json" -d '{"a": 1, "b": 2}' http://localhost
# -> [E]xecute, [D]escribe, [A]bort: e

Applying additional shell magic in our prompt, in this example passing file names to ffmpeg:

ls
# -> 1.mp4 2.mp4 3.mp4
sgpt -s "ffmpeg combine $(ls -m) into one video file without audio."
# -> ffmpeg -i 1.mp4 -i 2.mp4 -i 3.mp4 -filter_complex "[0:v] [1:v] [2:v] concat=n=3:v=1 [v]" -map "[v]" out.mp4
# -> [E]xecute, [D]escribe, [A]bort: e

If you would like to pass generated shell command using pipe, you can use --no-interaction option. This will disable interactive mode and will print generated command to stdout. In this example we are using pbcopy to copy generated command to clipboard:

sgpt -s "find all json files in current folder" --no-interaction | pbcopy

Shell integration

This is a very handy feature, which allows you to use sgpt shell completions directly in your terminal, without the need to type sgpt with prompt and arguments. Shell integration enables the use of ShellGPT with hotkeys in your terminal, supported by both Bash and ZSH shells. This feature puts sgpt completions directly into terminal buffer (input line), allowing for immediate editing of suggested commands.

Shell_GPT_Integration.mp4

To install shell integration, run sgpt --install-integration and restart your terminal to apply changes. This will add few lines to your .bashrc or .zshrc file. After that, you can use Ctrl+l (by default) to invoke ShellGPT. When you press Ctrl+l it will replace you current input line (buffer) with suggested command. You can then edit it and just press Enter to execute.

Generating code

By using the --code or -c parameter, you can specifically request pure code output, for instance:

sgpt --code "solve fizz buzz problem using python"
for i in range(1, 101):
    if i % 3 == 0 and i % 5 == 0:
        print("FizzBuzz")
    elif i % 3 == 0:
        print("Fizz")
    elif i % 5 == 0:
        print("Buzz")
    else:
        print(i)

Since it is valid python code, we can redirect the output to a file:

sgpt --code "solve classic fizz buzz problem using Python" > fizz_buzz.py
python fizz_buzz.py
# 1
# 2
# Fizz
# 4
# Buzz
# ...

We can also use pipes to pass input:

cat fizz_buzz.py | sgpt --code "Generate comments for each line of my code"
# Loop through numbers 1 to 100
for i in range(1, 101):
    # Check if number is divisible by both 3 and 5
    if i % 3 == 0 and i % 5 == 0:
        # Print "FizzBuzz" if number is divisible by both 3 and 5
        print("FizzBuzz")
    # Check if number is divisible by 3
    elif i % 3 == 0:
        # Print "Fizz" if number is divisible by 3
        print("Fizz")
    # Check if number is divisible by 5
    elif i % 5 == 0:
        # Print "Buzz" if number is divisible by 5
        print("Buzz")
    # If number is not divisible by 3 or 5, print the number itself
    else:
        print(i)

Chat Mode

Often it is important to preserve and recall a conversation. sgpt creates conversational dialogue with each LLM completion requested. The dialogue can develop one-by-one (chat mode) or interactively, in a REPL loop (REPL mode). Both ways rely on the same underlying object, called a chat session. The session is located at the configurable CHAT_CACHE_PATH.

To start a conversation, use the --chat option followed by a unique session name and a prompt.

sgpt --chat conversation_1 "please remember my favorite number: 4"
# -> I will remember that your favorite number is 4.
sgpt --chat conversation_1 "what would be my favorite number + 4?"
# -> Your favorite number is 4, so if we add 4 to it, the result would be 8.

You can use chat sessions to iteratively improve GPT suggestions by providing additional details. It is possible to use --code or --shell options to initiate --chat:

sgpt --chat conversation_2 --code "make a request to localhost using python"
import requests

response = requests.get('http://localhost')
print(response.text)

Let's ask LLM to add caching to our request:

sgpt --chat conversation_2 --code "add caching"
import requests
from cachecontrol import CacheControl

sess = requests.session()
cached_sess = CacheControl(sess)

response = cached_sess.get('http://localhost')
print(response.text)

Same applies for shell commands:

sgpt --chat conversation_3 --shell "what is in current folder"
# -> ls
sgpt --chat conversation_3 "Sort by name"
# -> ls | sort
sgpt --chat conversation_3 "Concatenate them using FFMPEG"
# -> ffmpeg -i "concat:$(ls | sort | tr '\n' '|')" -codec copy output.mp4
sgpt --chat conversation_3 "Convert the resulting file into an MP3"
# -> ffmpeg -i output.mp4 -vn -acodec libmp3lame -ac 2 -ab 160k -ar 48000 final_output.mp3

To list all the sessions from either conversational mode, use the --list-chats or -lc option:

sgpt --list-chats
# .../shell_gpt/chat_cache/conversation_1  
# .../shell_gpt/chat_cache/conversation_2

To show all the messages related to a specific conversation, use the --show-chat option followed by the session name:

sgpt --show-chat conversation_1
# user: please remember my favorite number: 4
# assistant: I will remember that your favorite number is 4.
# user: what would be my favorite number + 4?
# assistant: Your favorite number is 4, so if we add 4 to it, the result would be 8.