Create Alias Shortcuts for the Command Line on Linux and Mac

A guide to creating aliases for Bash

When working in the terminal it can sometimes be tedious to repetitively type a long sequence of commands to perform a set of regular actions. Instead, you may rather want to look at creating an alias which is like a shortcut that you can type to execute a longer sequence of commands.

This article will explain how you can create alias shortcuts for Bash, which is a Unix Shell that is widely used in Linux distributions and in Mac OS versions up to Mojave. 

Creating an alias

Here is the syntax that you can use to create a new alias in Bash.

alias new='mkdir new && cd new'

In the above example, new is the alias that executes the commands contained in the single quotes that follow. So in this case, if you now enter new into your command line it will create a directory called 'new' and change into the new directory. The && syntax is used to chain commands together, it basically means execute the command that follows if the preceding command completed without an error. If you want the following command to be executed regardless of whether an error occurred then you can use ; instead. The above is a simple example which you can adjust to streamline more complex sets of commands in your workflow.

You can also use aliases to overwrite existing commands. For example, if you want to have the rm command confirm before deleting a file you could add the following alias with the -i flag.

alias rm='rm -i'

Saving an alias

Typing the above commands into your command line will only create a temporary alias for that terminal session. If you want your alias to be remembered for future sessions then you need to add the above alias code to your ~/.bash_profile or ~/.bashrc file. These files that begin with a . are essentially hidden files (commonly used as configuration files) that are not shown in directory listings by default. The ~/ prefix is just shorthand for the current user's home directory which will help you point to the file's exact location. You can edit these hidden files with any text editor. Below we'll take a look at how you can find and edit these files using the GUI or command line.

Mac OS

If you're using Mac OS Mojave and earlier you can add your alias to your .bash_profile file. To find this file using the Mac OS GUI, do the following:

  • Switch to the Finder app
  • Select Go > Home from the top menu
  • Press CMD + Shift + . to show hidden files
  • Right click on the .bash_profile file and open it with your preferred text editor


To find and edit your .bash_profile file using the command line, do the following:

  • Open the Terminal app.
  • Enter nano ~/.bash_profile into your terminal to open the file using nano. You can also substitute nano with other text editors like vim.
  • Add the alias to the file, save and exit.
  • Restart the terminal for changes to take effect.


After saving your alias you will need to restart the terminal for the changes to take effect.

Linux

If you're using Linux you will need to add the alias to your ~/.bashrc file.

  • Open the terminal.
  • Enter nano ~/.bashrc into your terminal to open the file using nano. You can also substitute nano with other text editors like vim.
  • Add the alias to the file, save and exit.
  • Restart the terminal for the changes to take effect.

Listing aliases

To list all of your aliases you can simply type the following into your command line:

$ alias

Creating aliases for common commands is a good way to streamline your workflow and can reduce the tediousness of typing out long and repetitive commands.