nl-sh: The Natural Language Shell

A proof-of-concept for the future of the terminal

Mike Cvet
3 min readFeb 13, 2024
Photo by Jake Walker on Unsplash

I spend a lot of time in the terminal, and often have to search around Stack Overflow for the right combination of find or git or awk and whatever else to complete my tasks.

Although natural to myself and most engineers, the terminal is not a particularly friendly experience; it's efficient for experts, but for novices, it takes a long time to build expertise.

I built a proof-of-concept shell which accommodates both typical shell commands and usage, as well as natural human language expressions, indicating intent for a specific set of system operations. It integrates an LLM directly into the terminal, and I think it provides a natural way for both experts and novices to interface with their POSIX systems.

This shell is called nl-sh and is written in Rust.

Some examples, such as copying files from remote server to a local directory:

 > [nl-sh] /Users/mike/code/nl-sh $ copy remote directory /home/mike/log_data on remote host "log-server" to the local machine; store the data in a new directory called "log-server-data"
> scp -r log-server:/home/mike/log_data log-server-data Yes
mike@log-server's password:

Inspect and manipulate the local directory:

> [nl-sh] /Users/mike/code/nl-sh/log-server-data $ ls -lhs
total 861424
166928 -rw-r--r-- 1 mike staff 82M Feb 13 09:41 0.log
166928 -rw-r--r-- 1 mike staff 82M Feb 13 09:41 1.log
166928 -rw-r--r-- 1 mike staff 82M Feb 13 09:41 2.log
360640 -rw-r--r-- 1 mike staff 163M Feb 13 09:41 3.log
0 drwxr-xr-x 6 mike staff 192B Feb 13 09:46 log_data

> [nl-sh] /Users/mike/code/nl-sh/log-server-data $ concatenate all files in the current directory into a new file called combined.log
> cat * > combined.log Yes

> [nl-sh] /Users/mike/code/nl-sh/log-server-data $ list all files with human-readable sizes
> ls -lh Yes
total 1696064
-rw-r--r-- 1 mike staff 82M Feb 13 09:41 0.log
-rw-r--r-- 1 mike staff 82M Feb 13 09:41 1.log
-rw-r--r-- 1 mike staff 82M Feb 13 09:41 2.log
-rw-r--r-- 1 mike staff 163M Feb 13 09:41 3.log
-rw-r--r-- 1 mike staff 408M Feb 13 10:24 combined.log

Package up that directory and produce a checksum:

> [nl-sh] /Users/mike/code/nl-sh $ create a tarball archive of local directory "log-server-data", compress the archive, and then produce an md5 checksum of the resulting file
> tar -czf log-server-data.tar.gz log-server-data && md5 log-server-data.tar.gz Yes
MD5 (log-server-data.tar.gz) = c5721410d1dcd369b9ced04d3290567d

Here’s a video screen capture with some more examples:

How it works

Behind the scenes, nl-sh uses some heuristics to determine if the input is a valid system command; this is mostly for performance reasons. You can see in the video above that command-based inputs are pretty quick.

If the shell is not certain that the input is a valid command sequence, it constructs an LLM prompt, containing context about the local environment and operating system, and issues a request to OpenAI’s APIs for the GPT4 model. The model is instructed to provide a platform-specific valid command sequence after interpreting the user’s natural language prompt.

After validating the command with the user, the shell executes the command, wrapping it in the user’s environment $SHELL via a call through std::process::Command

This is just a proof-of-concept

This isn’t a real shell; it's not configurable, doesn’t support pseudo-terminal output, doesn’t read environment shell command history, and so on.

A production-ready variant would rely on a local, probably fine-turned LLM focused on POSIX system specifics. I tried Llama2 for this use case; it worked fairly well, but not quite as well as GPT4.

I think this demonstrates a natural way to bring the power of LLMs deep into the system-administration space in a way which doesn’t break the flow of the terminal operator.

--

--

Mike Cvet

I’m a former Distinguished Engineer at LinkedIn and Twitter, was an early engineer at a couple startups with successful exits, and hacked around at Red Hat