I took a look at Bavarder, a GTK ChatGPT app for Linux recently (note: this app also supports a number of other generative AI backends, fwiw). While it’s a well-made app that works well a few readers wondered what the point of using over the ChatGPT website was.
Enter, chatGPT-shell-cli. This, as its name should suggest, a command-line tool for asking ChatGPT questions or giving it tasks, and receiving the output right there, in the same terminal session.
Using ChatGPT from a terminal could be useful on say, a headless system when you need to find something out, or if your desktop session crashes and you’re left using a TTY, you can troubleshoot the issue.
It’s a bash script so works just about anywhere Linux does, though it makes use of both
jq so if your distro doesn’t come with those preinstalled, install them prior to trying this script.
Let’s take a look at some of the things this can do.
Use ChatGPT from a Terminal
Obviously, being a ChatGPT CLI app, the key role of this script is to enable you to ask ChatGPT a question or query, set it a task, grammar check your writing (hi), or, y’know, probing its disembodied, artificially predictive soul.
You run the script (if it’s installed you just type
chatGPT to run it) and enter a prompt. After a little but of thinking the script will return text. “Context” is enabled so you ask follow up questions, tell it refine or rephrase its output, and so on.
But it can do a few other neat things too.
Want to turn your text into art? Using this script you can — though, alas, you don’t get to see ingeniously rendered ASCII creations. Instead, the script generates the image from your description and ChatGPT gives you a link to view it in a web browser:
You can get ChatGPT to generate workable commands you can run on your Linux system.
To do this, run
chatgpt then, for your prompt, enter
command: followed by a description fo what you want to achieve. For example,
command: convert example.mp4 to output.mov using ffmpeg sped up by 50 percent.
Hit enter, and et voila: it prints what you need with a prompt to run it:
You can also set an initial prompt for ChatGPT. This allows you to make it answer according to specific conditions.
chatgpt -i "Your prompt conditions".
For example, if I run
chatgpt -i "Answer every question as a pirate" ChatGPT will answer all my queries in pirate speak — oo-arr, me ‘arties!
Use your imagination here; get creative, be entertained, let it make you more productive.
Generative AI services such as ChatGPT are not infallible. They can (and often do) produce convincing-sounding answers that are just plain wrong. Take everything it says with caution. If using this for academic work do double-check and verify information.
You can download ChatGPT-shell-cli from GitHub. The project is free, open-source software, and should work with minimal effort on most major Linux distributions.
A handy one-line installer command is listed on the GitHub project page. This sets the app up and prompts you to add an OpenAI API key (see below):
curl -sS https://raw.githubusercontent.com/0xacx/chatGPT-shell-cli/main/install.sh | sudo -E bash
You do need an OpenAI API key to use this script — though don’t let it put you off.
Create a free OpenAI account to generate an API Key. Copy the key — you only get to see this once so save it somewhere — then edit your
~/.bash_profile file, add
export OPENAI_KEY=your_key_here to the end, save, and restart your terminal session.
If that doesn’t work (it didn’t for me on Fedora) you can run
export OPENAI_KEY=your_key_here in a terminal to use the API key for the duration of your session (i.e. until you close the tab/window).