How to Create an Alias in Linux

Use aliases as shortcuts for Linux commands

HTML code on a black screen

Pixabay

 

Despite the advances of the graphical desktop for Linux, you cannot escape the shell altogether—and if you manage a Linux-based server, the shell is generally all you get. To make the issuance of common but potentially cumbersome commands easier, Linux includes an alias utility to offer shortcuts for these commands.

How a Linux Alias Works

The alias command creates a series of entries into the configuration file of the current shell—for example, in ~/.bashrc for Bash or ~/.zshrc for Zsh.

Although you're free to edit aliases directly within your shell's configuration files, you should use alias to avoid inadvertently mistyping something that could screw up your shell. Veteran Linux aficionados often keep two shells installed—e.g., both Bash and Zsh, or Csh, or Tcsh. Thus, if you screw up the config files for one shell, just create a new terminal session with the other shell to facilitate repairs.

Executed on its own, alias lists all the active aliases for the shell. Because the alias parameters are shell-specific, if you routinely use more than one shell you'll need to repeat your aliases in others.

When you execute a stored alias, the shell expands the original content in place of the alias shortcode. For most shells, including Bash, this functionality limits to command expansion. However, some other shells, including Zsh, support a more robust system of global substitutions for aliased content.

How to Create an Alias in Linux

linux alias

Create a new alias, regardless of your shell, with the following command:

alias shortcode="full_command_to_execute"

The shortcode represents the custom command you'll invoke from the shell prompt, and full_command_to_execute references the full command name. It's prudent to be explicit with the full command by including full pathnames instead of relative pathnames were relevant.

After you execute alias in this way, the command displays nothing unless you commit a syntax error. Assuming no errors, the newly aliased shortcode is ready to use immediately.

For example, to append new lines to a diary file, a diary alias could invoke cat with an append-rediection to that diary file. For example, the command:

alias diary="cat >> ~/Documents/diary.txt"

creates a diary alias that, when executed, puts the shell into text-edit mode. Add new content and press Ctrl+D to save it; the cat command appends that text to the end of the diary.txt file stored in your home Documents folder.

Changing and Removing Aliases

To change an alias, just redefine it. A new alias command using the same shortcode overwrites the full command of the original shortcode.

To remove an alias, use the unalias command, as follows:

unalias shortcode

It's prudent to run alias to see what aliases are already installed on your system. For example, people who favor Zsh and install the Oh-My-Zsh utilities automagically enjoy a bunch of standard aliases, and adding extensions to Oh-My-Zsh (e.g., the git integration) can add up to 100 more. Don't assume that just because you've never added an alias, that your computer doesn't have any aliases!