A command by any other name: The utility of aliases and wrappers in the shell

This isn’t a manifesto about anonymity, not that kind of alias, just a simple moments appreciation for decoupling programs from their invocations.

The title makes reference to the line “A rose by any other name would smell as sweet” from Shakespeare’s Romeo and Juliet.

For as long as I have made a home on the terminal I have made heavy use of aliases to make common tasks more efficient and less brittle.

Example, I wrote a script that switches all the theming on my system (window manager, terminal, editor, browser etc). Rather than typing out system-theme-switcher --dark when I want to switch to ‘dark-mode’ across my system, I have dark as an alias to this same command.

Click here to see the whole script

#!/bin/sh
# author:  Silas Jelley
# created: 2022-12-09
# updated: 2023-09-22 17:09:38 

# Place script somewhere on your PATH and make it executable.
# For convenience, create the following aliases:
#   alias light="system-theme-switcher --light"
#   alias dark="system-theme-switcher --dark"

# Themes and colors
dark_gtk_theme="Arc-Gruvbox"
dark_alacritty_theme="$HOME/.config/alacritty/themes/pencil-dark.yml"
dark_waybar_bg_color="#212121"
dark_wofi_bg_color="#212121"
dark_wofi_fg_color="#f1f1f1"
dark_wallpaper="#000000 solid_color"
dark_btop_theme="Default"
dark_neovim_theme="$HOME/.config/nvim/colors/gruvbox.vim"

light_gtk_theme="Default"
light_alacritty_theme="$HOME/.config/alacritty/themes/pencil-light.yml"
light_waybar_bg_color="#f1f1f1"
light_wofi_bg_color="#f1f1f1"
light_wofi_fg_color="#212121"
light_wallpaper="#999993 solid_color"
light_btop_theme="whiteout"
light_neovim_theme="$HOME/.config/nvim/colors/theme.vim"

if [ "$1" = "--light" ]; then
	prefer_color_scheme="prefer-light"
	gtk_theme="$light_gtk_theme"
	alacritty_theme="$light_alacritty_theme"
	waybar_bg_color="$light_waybar_bg_color"
	wofi_bg_color="$light_wofi_bg_color"
	wofi_fg_color="$light_wofi_fg_color"
	wallpaper="$light_wallpaper"
	btop_theme="$light_btop_theme"
	NEOVIM_THEME="$light_neovim_theme"
	FIREFOX_DARKMODE_SETTING="0"

elif [ "$1" = "--dark" ]; then
	prefer_color_scheme="prefer-dark"
	gtk_theme="$dark_gtk_theme"
	alacritty_theme="$dark_alacritty_theme"
	waybar_bg_color="$dark_waybar_bg_color"
	wofi_bg_color="$dark_wofi_bg_color"
	wofi_fg_color="$dark_wofi_fg_color"
	wallpaper="$dark_wallpaper"
	btop_theme="$dark_btop_theme"
	NEOVIM_THEME="$dark_neovim_theme"
	FIREFOX_DARKMODE_SETTING="1"

else
	echo "Mode not specified, options are --light, --dark"
	exit
fi

sed -i 's/ *background-color:.*/    background-color: '"$waybar_bg_color"';/' "$HOME/.config/waybar/style.css"
sed -i 's/ *background-color:.*/    background-color: '"$wofi_bg_color"';/' "$HOME/.config/wofi/style.css"
sed -i 's/    color:.*/    color: '"$wofi_fg_color"';/' "$HOME/.config/wofi/style.css"
sed -i 's/ *output \* bg.*.*/    output \* bg '"$wallpaper"'/' "$HOME/.config/sway/config"
sed -i 's/color_theme = .*/color_theme = '"$btop_theme"'/' "$HOME/.config/btop/btop.conf"
gsettings set org.gnome.desktop.interface gtk-theme "$gtk_theme"
gsettings set org.gnome.desktop.interface color-scheme "$prefer_color_scheme"
nohup pkill waybar_bg >/dev/null 2>&1 &
swaymsg reload >/dev/null
cp "$alacritty_theme" "$HOME/.config/alacritty/themes/auto.yml"
cp "$NEOVIM_THEME" "$HOME/.config/nvim/colors/auto.vim"
nvim --server ~/.cache/nvim/server.pipe --remote-send ':colorscheme auto'
touch "$HOME/.config/alacritty/alacritty.yml"
sed -i "s/user_pref(\"ui.systemUsesDarkTheme\",.*);/user_pref(\"ui.systemUsesDarkTheme\",$FIREFOX_DARKMODE_SETTING);/" ~/.mozilla/firefox/silas/prefs.js

In the above case that’s a simple matter of putting the following in my shell configuration:

  alias light="system-theme-switcher --light"
  alias dark="system-theme-switcher --dark"

After reading something a long while back by Drew Devault, I also began using wrapper scripts over the programs I use often. Primarily this is done so that I can call these programs with the options I almost always use without having to repeat myself.

One such wrapper I use often is one over networkmanager so I don’t have to remember its somewhat verbose command structure. And I alias this to wifi allowing me to type wifi connect, select from available networks and be prompted to enter a password. For my most common networks I have custom handling, so wifi home connects me to my home network, automatically retrieving the password from my password manager.

Click here to view the script

#!/bin/sh
# creator:  Silas Jelley
# created: 2019-06-15 13:12:04
# updated: 2022-12-09 07:04:28
# Without completion nmcli is damned inscrutable, and even with completion it's
# a bit on the arcane side. Here's my little helper wrapper.

# DEPENDENCIES: nmcli, fzf, pass, sed etc

# Place script somewhere on your PATH and make it executable.
# For convenience, create the following alias:
#   alias wifi="system-wifi-wrapper"


if [ "$1" = "list" ]; then # List available wifi networks
    nmcli device wifi list

elif [ "$1" = "connect" ]; then # Connect to specified network, else provide 
    if [ "$2" != '' ]; then     # a fuzzy selection of available networks.
        WIFI_SSID="$2"
    else
        WIFI_SSID="$(nmcli device wifi list | sed 's/^... *//' | sed 's/^USE *//' | fzf | awk '{print $1}')"
    fi
    nmcli device wifi connect $WIFI_SSID --ask

elif [ "$1" = "status" ]; then  # Show device status
    nmcli device status

elif [ "$1" = "off" ]; then     # Turn off the wifi radio
    nmcli radio wifi off

elif [ "$1" = "on" ]; then      # Turn on the wifi radio
    nmcli radio wifi on

elif [ "$1" = "ip" ]; then      # Show IPv4 address
    nmcli device show wlp0s20f3 | grep IP4.ADDRESS  | sed 's/^.*: //' | awk '{$1=$1};1'

elif [ "$1" = "house" ]; then   # Connect to house wifi
    WIFI_SSID="Orcon-Wireless"
    WIFI_PASSWORD="$(pass show misc/locking-street | grep primary | sed 's/^.*: //')"
    nmcli device wifi connect $WIFI_SSID password $WIFI_PASSWORD

elif [ "$1" = "cabin" ]; then   # Connect to cabin wifi
    WIFI_SSID="Jelleybean"
    WIFI_PASSWORD="$(pass show misc/locking-street | grep extender | sed 's/^.*: //')"
    nmcli device wifi connect $WIFI_SSID password $WIFI_PASSWORD
else
    echo "Input invalid"
fi

I also occasionally torrent resources that I’m not able to find in print, or those that I own in print but wish to have a digital copy of. When I recently switched from using aria2 to rtorrent for this purpose all I had todo was update my wrapper script with a call to rtorrent, adding a couple of options, in order to continue using torrent path/to/torrent.file. No need to learn and remember a new tools syntax!