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.
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 (
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
- 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
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.
- fq jq for binary files
- 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
- fx - JSON viewer tool, similar to jq
- 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
- yt-dlp Fork with additional features and extractors
Appending multiple lines to a file
tee -a FILE << EOF > /dev/null echo "line 1" echo "line 2" EOF
Start custom scripts with ‘,’
If you create custom scripts in a
bin directory, prepend them with a
, to keep them separate from the standard binaries and also allow easy access via tab completion4.
Improving command line ergonomics
You can choose a variety of colors to your suiting.
- 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 tools5, 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
- Font options: you can patch your font to add icons that will be displayed on the command line using NerdFont.
- Tyler Cipriani has several easy functions, like calc and temperature that can be added for quick reference on the command line6.
- GameShell is a game teach the Unix shell