Tags All Entries

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.

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

Scripting

Appending multiple lines to a file

tee -a FILE << EOF > /dev/null
echo "line 1"
echo "line 2"
EOF

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
  • fq jq for binary files
  • pandoc
  • xargs for relocating piped output to different spots within the following command
  • 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
  • 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
  • tv - csv pretty printer for the command line
  • rq - record analysis and transformation
  • youtube-dl
    • yt-dlp Fork with additional features and extractors

Improving command line ergonomics

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
  • 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.
2.
3.
Hockenberry, C. Jsc: My New Best Friend furbo.org. (2021).
4.
Hess, J. Moreutils.
5.
Cipriani, T. Cool desktops don’t change. (2022).