When working on Linux and Unix systems, you’ll often find yourself using long shell commands that you repeat several times per day (or per session). Things like
- checking on the current resource usage of a system,
- checking if a process has completed yet, or
- using a complex group of commands, piped together with special options to give you just the output you want.
Generally, you’ll either use CTRL+R to search for these past commands in the shell’s history. Some of you may even copy useful commands to a special file that you can easily reference. You might even just enter them manually each time you need them, since you’re too lazy to be bothered.
There’s a simple solution that covers all of these cases well: Aliases.
How to create shell aliases
A shell alias can be created by entering alias =”<command>
Something I usually have on my systems is an alias called lr, which extends to the ls -lrc command. To create this shell alias, I’d type:
alias lr="ls -lrc"
Typing this into a shell makes it instantly usable in your current shell session (see the “Persisting Aliases” section for information on how to make aliases permanent). Simply type in lr afterwards to see the result.
There are cases where you want to shadow other commands. This is also a possible. Shadowing means that there is already a command with this name and you “overwrite” that with something else. When you shadow a command, you’re not really overwriting any files — you’re just telling the shell that when this command is entered, you actually want to do something other than the default action.
There are two common use cases. One is a generic way for running the command with “default options”, so in case of ls you might want to make it always list all files (-a) and use colored output (–color).
alias ls="ls -a --color"
The next time you use ls it will show you output in color (on most systems this is the default, so you won’t notice a difference). The -a option however means that hidden files, starting with a dot and . (a dot symbolizing the current directory) and .. (symbolizing the containing directory) will be shown.
Another typical use case for shadowing is drop-in replacements: commands that do the same thing in a better (or simply preferred) manner. Often you don’t want to (or cannot) delete the original program, so tricking with symlinks isn’t an option. Also, why do something complicated when you don’t need to?
Example: I prefer to use nvim (neovim) as an improved default version of vim on my system, there is an alias from vim to nvim. It simply looks like this:
Listing existing aliases
You can easily get lost on a system with many aliases, so I don’t recommend creating too many of them. This is especially true when you start managing dozens or hundreds of systems in an environment. You usually want those systems to have a predictable, minimal environment so that you don’t spend all your time on special configuration. After all, no one likes troubleshooting problems that happen because a special magic custom thing (alias, software, etc.) is missing from the environment on a few of your thousand servers.
That said, sometimes you want to have a list of all aliases in your current environment. Calling the alias command with no arguments gives you exactly that — a list of all currently active aliases.
To remove an alias simply run unalias <aliasname>. For example, to remove the ls alias we created earlier, you could just run:
Running the alias command in a shell won’t make that alias permanent. This means that when you open a new terminal window, none of the aliases from another window will be available. You’d have to create that same alias in every new shell session, which is silly.
To make aliases persistent, add them to your shell’s startup file. If you are using bash, that’s the .bashrc file in your user’s home directory. If you’re using zsh it is ~/.zshrc. For other shells, just check your shell’s manual page (enter man <shellname>).
You can check which shell you are currently using by typing in echo $SHELL. In my case it looks like this:
echo $SHELL /bin/zsh
If I now use man zsh and scroll down to the FILES section, I will find that this shell startup file is called .zshrc. Your best guess is something like .<shellname>rc, which likely already exists in your user’s home directory.
This is a basic introduction to creating and managing shell aliases, and has hopefully illustrated their many uses. For more details on shell features, you’ll want to check out the tutoriaLinux YouTube channel.