To run ShellGPT you must install it and obtain a ChatGPT API key.

Want to use the ChatGPT in a terminal window on your Ubuntu Linux PC?

ShellGPT lets you use all the features of the famous AI chatbot, on the command line.

Introducing ChatGPT

Hannah Stryker / How-To Geek

Here’s how to set ShellGPT up and start using it.

What Is ShellGPT?

ShellGPTis a Python program that lets you accessOpenAI’s ChatGPTfrom the command line of a terminal window.

Checking the version of Python in a terminal window

It sends your text prompts and your ChatGPTAPIkey to ChatGPT and prints out ChatGPT’s response.

Just keep in mind that it’s a simulation of a knowledgeable, intelligent person.

It isn’t genuinely intelligent, andit’ll happily make stuff up.

Checking the version of pip in a terminal window

If Python isn’t installed, you’ve got the option to install it using this command.

We also needpip, the python package manager.

It wasn’t installed on our test computer, so we added it usingapt.

Installing pip on Ubuntu

We’re going to install ShellGPT in aPython virtual environment.

It’s just a safe precaution.

To do that, we’re going to need to set up the Python virtual environment packages.

Installing the Python virtual environment module on Ubuntu

Step 2: Prepare a Python Virtual Environment

Create a directory to install ShellGPT into.

We named ours “shellgpt”, just to keep things obvious.

Change into your new directory when it’s been created.

Creating the shellgpt directory

We created one called “shellgpt.”

This gives us a private, virtual environment called “shellgpt”, inside our “shellgpt” directory.

To activate it we need to run a script called “activate.”

Generating the shellgpt virtual environment

This is located in the “bin” directory of our virtual environment.

Note the “(shellgpt)” in front of the command prompt.

On the API keys web page, smack the “Create New Secret Key” button.

Activating the Python shellgpt virtual environment

Your new key is displayed to you.You only get to see your key this one time.

Step 4: Export the API Key

We need to make the key available to ShellGPT.

The easiest way is to export it as an environment variable.

The ChatGPT account menu with the “View API Keys” option highlighted

store your changes and terminate the editor.

To force your terminal session to reread your “.bashrc” file, use thesourcecommand.

The installation will begin, and a variety of package names will scroll by in your terminal window.

The ChatGPT “Create new secret key” button

When it’s finished we can, finally, use ShellGPT to access ChatGPT.

Step 6: Use ShellGPT

The ShellGPT command issgpt.

We provide our text prompts to this command, and press “Enter.”

providing a name for the API key, with the “Create secret key” button highlghited

ShellGPT acts as the middleman between us and ChatGPT, and displays the response from ChatGPT.

Related:How to Create Aliases and Shell Functions on Linux

That’s great, it’s all working.

A better way is to create an alias that does all of that for us.

Exporting the ChatGPT API key on the command line

Edit your “.bashrc” file and add this line.

Remember to use the names of the directory and virtual environment that you created.

preserve your changes, and usesourceto read the “.bashrc” file again.

Adding the export ChatGPT line to the .bashrc file

Learning ShellGPT Commands

TheShellGPT GitHub pagehas much more information on using ShellGPT and its command line options.

For example, the–codeoption limits the output of ShellGPT to show program code only.

By adding the–codeoption, the description is suppressed.

Using source to force the shell to read the .bashrc file

This would be handy if you want to redirect the output into a file.

ShellGPT, through ChatGPT, can also generatesyntax-correct Bash commands and shell scripts.

Or at least, that’s what it says.

Using pip to install the shell-gpt module

Output during the installation of the shell-gpt module

Using the sgpt command to send input to ChatGPT

Adding an alias to the .bashrc file

Using source to force the shell to read the .bashrc file

Using the chatgpt alias to access ShellGPT

Using ChatGPT to generate a recursive function in C

Using ChatGPT to generate a recursive function in C, with the –code option so only the function code is displayed

Using ShellGPT and ChatGPT yto write the closing paragraph