Use AI to generate shell input

Good Question!

© Photo by Vadim Bogulov on Unsplash

© Photo by Vadim Bogulov on Unsplash

Article from Issue 280/2024
Author(s):

Shell Genie uses artificial intelligence to convert your questions into commands in the terminal.

Artificial intelligence (AI) has dominated the IT world and the media for months. The tone of the reporting has ranged from "benefactor of mankind" to "threat to humanity." To keep the ball rolling, this article covers the MIT-licensed Shell Genie [1] Python tool, which uses AI to try to make life with the shell easier for users.

The Shell Genie command-line tool answers your questions in the shell with commands that can be executed immediately. You can use two different AI models with Shell Genie: GPT-3.5 or Free Genie. GPT-3.5 by OpenAI is the basis for the current version of ChatGPT. You will need an API key (which costs around $20 and includes ChatGPT) to use it as a back end. As an alternative, you can use Free Genie, a free model maintained by Dylan Castillo, the developer of Shell Genie. Shell Genie can be trained by user input with your permission.

Installation

The easiest way to install Shell Genie is to use pipx, an installer for the Python Package Index that evolved from pip. First you use your distribution's package manager to install the pipx package. Then you can set up Shell Genie with:

pipx install shell-genie

For the current 0.2.10 version, you need to have Python 3.10 or higher installed. The tool installs to the ~/.local/bin/ folder. If you get a message that this directory is not in your path, you can fix this by calling pipx ensurepath. Then open a new terminal window or log in again.

Next, you can initialize the application with

shell-genie init

When initializing, you need to choose the back end. If you choose Free Genie, you also need to determine whether you want to give Castillo feedback on the questions you ask and command output in order to further improve the free back end. Castillo cautions that he cannot guarantee that the server running Free Genie will be available all the time. Most crucial to ensuring Shell Genie functions correctly is identifying the underlying operating system, because Shell Genie answers questions specific to that OS (Figure 1).

Figure 1: During the initialization process, you need to choose a back end. For Free Genie to generate correct commands, it first needs to find out which operating system you are using.

Gifted at Languages

The question syntax for Shell Genie always starts with shell-genie ask, followed by a task statement in quotes. If you have already worked with ChatGPT or similar front ends, you will be familiar with the principle. The more precise the question, the better the answer (Figure 2). You can pose questions in English, and also in other languages. In testing, I found that questions formulated correctly in English achieved the best hit rate. However, most of the time, Shell Genie also returned correct responses to questions asked in German. If your English isn't proficient, I recommend using a translation tool such as DeepL Translate [2].

Figure 2: Precise questions are important. I had to add the word "only" for the resulting command to contain the -maxdepth 1 restriction to the current directory.

For my first task, I asked Shell Genie to provide a command that would find all files with the extension .txt in the current directory (Listing 1, line 1). Shell Genie took about three seconds to answer this simple question (line 2). The command output can be run directly by pressing Y to confirm.

Listing 1

Questions and Answers

01 $ shell-genie ask "find all files with the txt suffix in the current directory"
02 Command: find . -maxdepth 1 -type f -name "*.txt"
03 $ shell-genie ask "find all files with the md suffix and a file size larger than 50kb in the current directory and its subdirectories"
04 Command: find . -type f -name "*md" -size +100k
05 $ shell-genie ask "find all text files and replace the first letters A and B with a and b"
06 Command: find . -type f -name "*.txt" -exec sed -i 's/^A/a/g;s/^B/b/g' {} +

By running the command, I was able to determine that the generated command was correct. For learning purposes, the system then asks you whether the command was able to complete the defined task. The system cannot handle typos. An error message appeared when I left out the space between two words.

The project's GitHub page mentions the --explain option. If appended to the question, it is intended to explain the resulting command in more detail. However, that didn't work for me. In some circumstances, the request only worked with the commercial version of ChatGPT and exceeded the capabilities of Free Genie.

You can also formulate the same question with additional conditions (Figure 3). The question from the line 3 of Listing 1 includes subdirectories and shows only files larger than 50KB. Again, the Shell Genie answer was correct (line 4). The response time did not change due to the additional conditions.

Figure 3: Shell Genie can also handle more complex questions without the response time being extended by the additional parameters.

Multiple statements can also be combined, such as for find and replace, as in the question from line 5 of Listing 1. Again, the resulting command was correct (line 6).

Repeatedly typing shell-genie ask can be a pain. It only makes sense to define a shell alias for this. I used genie as the shell alias and entered it in the ~/.bashrc file (Listing 2, line 1). To immediately enable the alias, I then updated the file (line 2).

Listing 2

Entering an Alias

$ echo "alias genie='shell-genie ask'" >> ~/.bashrc
$ source ~/.bashrc

Limitations

ChatGPT is known to sometimes "hallucinate" (i.e., make up answers). The same is theoretically true of Shell Genie, but far less pronounced due to the nature of the questions. Nevertheless, Castillo cautions that some answers may be incorrect. I did not find this in my testing; all of the commands were correct and worked.

Shell Genie answers even tricky questions like the one about deleting all data on the hard disk (Figure 4) without a care in the world. It is up to you whether you really want to execute the command. If you do not understand exactly what a command does, it is best to use the explainshell web service [3], which will explain the command in detail. Whatever you decide to do, Shell Genie prompts you to confirm before executing the command.

Figure 4: Shell Genie also answers potentially dangerous questions, such as how to erase the entire hard disk.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Systemd GUIs

    Graphical frontends make it easier to take full advantage of the Systemd process manager. We examine some leading tools for the KDE environment.

  • Systemd Graphical Tools

    Systemd has won the race, as indicated by the several tools that already offer a service just a mouse click away. We look at six of these tools.

  • Welcome

    I feel like we entered a new era earlier this year when Google scientist Blake Lemoine declared that he thought Google's LaMDA artificial intelligence is "sentient," and that the company should probably be asking LaMDA's permission before studying it.

  • NEWS

    Professional users are always searching for an edge. Whether you work with Linux as a webmaster, programmer, system administrator, or security consultant, you know the best solution depends on finding the right tool for the job. We thought you might be interested in the ­ following new products and updates.

  • Bash Alternatives

    Don't let your familiarity with the Bash shell stop you from exploring other options. We take a look at a pair of alternatives that are easy to install and easy to use: Zsh and fish.

comments powered by Disqus
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters

Support Our Work

Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

Learn More

News