Command Line

Tags: command-line

Command line broadly means a text based prompt for running software on a computer. I am primarily concerned with Unix-like shells, rather than say Microsoft/Window’s command line interfaces such as PowerShell. In modern GUIs, you often will be using a terminal emulator in lieu of a direct terminal for accessing the command line. An important convention in the command line is configuration via dotfiles.

Choosing a shell

Choosing between the various unix-compatible shells is important for day-to-day interactions with the command line and also compatibility of any shell scripts you write. In most cases, and most scripts, the shells will behave similarly and so the choice boils down to user preference and audience/usage for shell scripts.

/bin/sh is the system shell, classically it was the bourne shell, and the assumption is that it will be bourne shell compatible. Depending on the unix distribution, it may be the bourne shell, Almquist shell (ash), on Ubuntu it may be dash1 (a port of the ash shell), or it may be the bourne again shell (bash).

For day-to-day interactions

For day-to-day interactive use on the command line, most people will select a superset shell of sh that provides more features and niceties. Some options:

  • bash, is ubiquitous and in many cases people will refer to the shell as bash. bash is a common default on many distributions and *nix like environments.
  • fish is a feature rich shell with many of those built-in features target at interactivity and usability.
  • zsh is my personal preference and became the default for macOS with Catalina2.

For Shell Scripting

Selecting a shell for scripting depends on the expected execution environment for the scripts. If it is for your internal use, just use the your own shell or bash. If you are trying to maximize portability, target ksh or ash.

Notable mentions

  • The C shell (csh), or the improved version tsch, is the default for root users on FreeBSD, while the Almquist shell (ash) is the default for users.
  • KornShell (ksh) is favored by some for scripting for it’s portability/stability

Specific Tools

Essentials

There are a hand full of tools that must be learned in order to be proficient at the command line. These are largely around navigating or basic file manipulation. Use cd for changing directory and navigating across file system. Once in a directory, use ls to list the contents of a given directory and attributes about the files within it. Alternatively, the find command for finding files within a file tree. Once a file is located, cat for outputting a file to STDOUT, although you are more likely better off using a paging a program such as less if you just want to read the contents of a file. You can copy the file with cp or move/rename it with mv. At any point, if you get lost, man can be used to get the manual page of any command. Many people favor tldr for this purpose as it gives quick usable examples. grep for finding text within files and standard output. echo will output whatever is passed, this is most useful for inspecting environmental variables outside of scripting. For example, echo $SHELL will output the currently set shell. ssh is used to for connecting to remote systems securely (secure shell). rsync to synchronize two locations (can be local or remote) efficiently.

Advanced

  • tmux
  • awk
  • jq
  • pandoc
  • xargs for relocating piped output to different spots within the following command
  • pv is the pipe viewer tool for monitoring the progress of data through a pipeline
  • alt - Find alternate path for a provided path
  • tokei - Code statistics
  • direnv - set directory specific environment

Highly Specific

  • bandwhich
  • 7zip
  • exiftool
  • fx - JSON viewer tool, similar to jq
  • just
  • jsc
    • Javascript commandline tool bundled with macOS 3
  • johnkerl/miller
    • Miller is like awk, sed, cut, join, and sort for name-indexed data such as CSV, TSV, and tabular JSON
  • navi
  • tv - csv pretty printer for the command line
  • topgrade - tool for keeping up to date across multiple package managers
  • youtube-dl
    • yt-dlp Fork with additional features and extractors

Scripting

Improving command line ergonomics

You can choose a variety of colors to your suiting.

Replacements for common command line tools

  • RESH (Rich Enhanced Shell History) adds better history ergonomics to zsh and bash
  • Tealdear is a rust implementation of tldr for CLI cheat sheets
  • navi is a rust cheatsheet manager
  • Starship is a prompt across terminals
  • moreutils is a set of useful commandline tools4, one of which is sponge for reading the entire input before preceding
  • Pure is a minimal zsh prompt
  • powerlevel10k is a zsh theme
  • zoxide, fasd, z
  • dot
  • fzf
  • just - a command line runner. Useful when you have a handful of commands you run in a project folder regularly. Like a simpler make because it is not a build system
  • Font options: you can patch your font to add icons that will be displayed on the command line using NerdFont.
    • Fonts are largely a matter of personal preference, however it’s worth considering adding ligature support. Some fonts worth checking out are FiraCode and PragmataPro
  • Tyler Cipriani has several easy functions, like calc and temperature that can be added for quick reference on the command line5.

Tutorials


References

1.
DashAsBinSh - Ubuntu Wiki. Ubuntu wiki at https://wiki.ubuntu.com/DashAsBinSh.
2.
Use zsh as the default shell on your Mac. Apple support at https://support.apple.com/en-us/HT208050.
3.
Hockenberry, C. Jsc: My New Best Friend furbo.org. at https://furbo.org/2021/08/25/jsc-my-new-best-friend/ (2021).
4.
Hess, J. Moreutils. at https://joeyh.name/code/moreutils/.
5.
Cipriani, T. Cool desktops don’t change. at https://tylercipriani.com/blog/2022/06/15/choose-boring-desktop-technology/ (2022).

Links to this note