Why CLI?
It is a little surprising to me how strong my preference for solving technical problems with shell scripting and pipelining is, when I know that more powerful, more interesting, equally ubiquitous, alternatives exist.
I refer to "the cli" somewhat generically here.
I really specifically mean POSIX shells like zsh
, sh
or bash
This document is an attempt to enumerate the specific reasons I reach for the CLI, and shell scripts.
The cli is immediate
It is very easy to run a command or open a file.
Contrast to heavyweight languages, where running a command involves explicitly popping open a subprocess.
The cli "hides" that, it just does it.
The cli is persistent
If I need something to "live forever", I write it to a file. Simple.
The cli is flat
A shell script is, generally, executed the way that I read a book.
Context flows downward, and to the right
I can usually guess what a piped command is doing, based on the evolution of commands in the pipe.
The cli is right there.
If I need a cli I can go and get one
It's built into my editor (neovim), and copy/pasting into it is usually easy.
Opening a terminal emulator with a tmux-session running is literally a keybind away for me
The cli is noisy.
When I need to know what's going on, I can find out.
Running commands generally tell me as much as I need to know, or I can ask them to tell me more
If I really really need to know more, there's always strace
The cli is transparent
It's all text, basically. If I can make it into text, I can work with it.
The cli is well-known
This really pertains more to something like sh
or bash
or, (worst case) powershell
People will generally not freak out if I write a bash script
Even if I sneak in a very specialized tool like jq, I can get away with it as long as:
- The tool is readily available
- I say exactly what the tool is doing, and how
The cli is adaptable
If I need the objects-first capabilities of something like powershell
, I can pipe json to jq
If I need to persist some structured information I can use intermediate files, which, conveniently, can be inspected outside of shell scripts.
I will have to admit this is rather clunky, though.
The cli is interactive
It's a transparently writeable execution environment.
TUIs are back in style
I can use my keyboard for everything
Tab-complete rules
Command history fucking rules
The cli is scriptable
Scripts look like sequences of commands, because that's what they are.
The cli is familiar
Oh, right, I've been writing shell scripts more-or-less professionally for over 10 years now.
Hm.