Use AI to generate shell input
Good Question!
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).
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].
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.
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.
Buy this article as PDF
(incl. VAT)