Access ChatGPT Prompt in your Linux Terminal

Linux TLDR
Last Updated:
Reading time: 5 minutes

As Linux users, we primarily rely on the Linux terminal and prefer to use GUI alternatives to the CLI like Ranger (a CLI based file manager) or w3m (a CLI based web browser).

In the ongoing buzz of LLM models like ChatGPT, accessing them via a web browser is required. However, a GitHub user name 0xacx created a project called chatGPT-shell-cli to make it easier to access ChatGPT prompts via the CLI.

In this article, you will learn how to access the ChatGPT Prompt in your Linux terminal.

Tutorial Details

DescriptionAccess ChatGPT Prompt in your Linux Terminal
Difficulty LevelModerate
Root or Sudo PrivilegesNo
OS CompatibilityUbuntu, Manjaro, Fedora, etc.
PrerequisitesFree or Paid OpenAI Account
Internet RequiredYes

What is the ChatGPT-Shell-Cli?

The chatGPT-shell-cli is a lightweight shell script that uses the ChatGPT model β€œgpt-3.5-turbo” and all other OpenAI models with the β€œcompletions” endpoint and the β€œimages/generations” endpoint for generating images.

In layman’s terms, you can access the ChatGPT prompt the same way you do in your web browser. However, you have to keep in mind that this script uses an OpenAI API key, so to use it, you must have an OpenAI free or premium account.

Features

In terms of features, you will have almost all the elements that ChatGPT prompt offers, with a few additional ones listed below.

  • Communicate with ChatGPT via terminal.
  • Generate images from the text prompt.
  • View your chat history.
  • List all OpenAI models.
  • Set the OpenAI request parameters.
  • Pass the input prompt with a pipe, as a script parameter, or in normal chat mode.

With that, let’s check out the installation process.

Installation

There are two ways to install this chatGPT-shell-cli: either using the curl command or manually placing the script file on your system and giving the path to environment variables. Arch users can directly install it from their default package manager.

Now, before we start the installation process, let’s create an OpenAI account. If you already have one, then visit OpenAI and click on the β€œCreate a new secret key” button.

Creating a new API key on OpenAI

Now, it will ask for a name for the given secret key (optional).

Giving a name to the secret key

Lastly, it will show you the secret key (only the last time); simply copy and save it somewhere else for later use.

⚠️
Never share your secret key with anyone.
Copying the OpenAI secret key

Now, let’s check out all the steps to install this script on your Linux system.

Debian, Ubuntu, or Pop!_OS

Execute the following commands and provide your OpenAI API key when asked (got an error!).

$ sudo apt install curl jq -y
$ curl -sS https://raw.githubusercontent.com/0xacx/chatGPT-shell-cli/main/install.sh | sudo -E bash 

And then you need to set your OpenAI API key to use this script.

$ echo "export OPENAI_KEY=[You-OPENAI-API-KEY]" >> ~/.bashrc
$ source ~/.bashrc

Red Hat, Fedora, or AlmaLinux

Run the aforementioned commands, and when prompted, enter your OpenAI API key (got an error!).

$ sudo dnf install curl jq -y
$ curl -sS https://raw.githubusercontent.com/0xacx/chatGPT-shell-cli/main/install.sh | sudo -E bash

And then you need to set your OpenAI API key to use this script.

$ echo "export OPENAI_KEY=[You-OPENAI-API-KEY]" >> ~/.bashrc
$ source ~/.bashrc

Arch, Manjaro, or EndeavourOS

Directly issue the Yay command, and it will install the script for you.

$ yay -s chatgpt-shell-cli

And then you need to set your OpenAI API key to use this script.

$ echo "export OPENAI_KEY=[You-OPENAI-API-KEY]" >> ~/.bashrc
$ source ~/.bashrc

Manual Installation

While following the installation instructions for Debian and Red Hat based distributions, I encountered the following error.

Error while installing "chatGPT-shell-cli" via Curl

However, if you encounter the same error as me, you can try following the steps below.

1. Install the wget package based on your Linux distribution.

$ sudo apt install wget -y                                                                           #For Debian, Ubuntu, or Pop!_OS
$ sudo dnf install wget -y                                                                           #For Red Hat, Fedora, or AlmaLinux

2. Create a scripts directory in your home directory and download the shell script in that directory using the wget command.

$ mkdir ~/scripts
$ cd ~/scripts
$ wget https://raw.githubusercontent.com/0xacx/chatGPT-shell-cli/main/chatgpt.sh

3. Add the β€œscripts” directory path to your $PATH environment variable.

$ export PATH=$PATH:~/scripts/

4. Add the OpenAI API key to your shell profile by executing the following command:

$ echo "export OPENAI_KEY=[You-OPENAI-API-KEY]" >> ~/.bashrc
$ source ~/.bashrc

Tada!!! You have successfully installed the β€œchatGPT-shell-cli” script on your Linux system.

Accessing the ChatGPT Prompt in your Linux Terminal

Now, before doing anything else, execute the following command to ensure that everything is working correctly and we are getting the ChatGPT prompt in our terminal.

$ chatgpt

Output:

Launching the ChatGPT CLI prompt

As you can see from the above picture, in my system it’s perfectly installed. Note that when you write something to the prompt in a free account, you might get the following error:

OpenAI quota is exhausted

This is the limitation of the free account; we cannot do anything to resolve this except wait or buy the premium membership.

However, if you have access, you can directly start using it. For example, in the following prompt, I’ve asked the ChatGPT to β€œWrite a paragraph about how SystemD is bad, in the style of Donald Trumpβ€œ, and look what I got in return.

Communicating with ChatGPT CLI Prompt

Quite amusing, right? Now in the next prompt, I’ve asked the ChatGPT CLI prompt to β€œWrite me a hello world program in C, C++, Python and Java” and look what I got in return.

Asking programming questions in the ChatGPT CLI Prompt

Now, you can also use the following commands to perform specific things:

CommandsUsage
image:Generate an image by using the β€œimage: [describe your image]” command. If you are using iTerm, you can view the image directly; otherwise, it will provide you with a URL to view the generated image in the browser.
historyPrompt you with your chat history.
modelsGive you a list of all the available models for the OpenAI API.
model:Check the detailed description of a specific model by using the β€œmodel: [model ID shown in models command]” command.
command:It will interact with your system, you can use the β€œcommand: [list the current directory content]” command as an example.

You can check commands on your own, as they are easy to do and self explanatory. However, I’ve shown you a few examples of image commands.

So, in the following prompt, I’ve asked ChatGPT to generate an image of β€œluffy fighting with gold d. roger” and look what I’ve got.

Generating an image of β€œLuffy fighting to Gold D. Roger” in ChatGPT CLI Prompt

And now look at the following result:

Luffy is fighting against Gold D. Roger

Well, I didn’t expect it, but let’s let it go after all; it’s luffy. In another prompt, I asked ChatGPT to generate a β€œcat liking its paw”, and this was quite satisfying.

Generating an image of β€œCat Liking its Paw” in ChatGPT CLI Prompt

And the result is:

Cat Liking its Paw

This time, I can fairly say it was quite promising. I’ve tried and tested it until my quota is finished, and it was quite an impressive project.

Like, you can use the following command to pipe your query to the ChatGPT CLI prompt:

$ echo "What is the command to get all pdf files created yesterday?" | chatgpt

Or, you can pass your query as a command-line argument using the β€œ-p” flag.

$ chatgpt -p "What is the regex to match an email address?"

The following are lists of known flags (or parameters) that you can use.

Flags/ParametersDescriptions
β€œ-t” or β€œ--temperatureβ€œSet an temperature
β€œ-m” or β€œ--modelβ€œSpecify the model
β€œ--max-tokensβ€œSpecify the max number of tokens
β€œ-s” or β€œ--sizeβ€œSpecify the image size (accepted are 256Γ—256, 512Γ—512, 1024Γ—1024)
β€œ-p” or β€œ--promptβ€œPrompt
β€œ--prompt-from-fileβ€œSpecify the file containing the prompt in your system

Now, let’s end this article here.

I hope you find this article interesting; however, if you have any questions or queries related to this topic, feel free to ask them in the comment section.

Till then, peace!

Join The Conversation

Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.

2 thoughts on β€œAccess ChatGPT Prompt in your Linux Terminal”

    • If you’ve installed it via the installation script on Debian, Ubuntu, RedHat, Fedora, etc., then remove the β€œchatgpt” script file from the β€œ/usr/local/bin/chatgpt” location. If you’ve installed it manually, then remove the script from where you downloaded it and delete the added PATH in your shell configuration file.

      Reply