Bash Alias: How It Works and Why You Need One

hands typing on a laptop

hands typing on a laptop

The bash shell incorporates some of the best features of the C and Korn shells, such as job control, directory manipulation, and aliases. 

Aliases are very helpful to users who often type long commands or search their bash histories for a command they typed earlier. 

A bash alias is a shortcut mapped to a command that circumvents the need to remember or type it repeatedly. You can quickly set short and memorable aliases for the long commands you use every day quickly. 

This article explains how bash aliases work, why you should consider using them, and how to create them in a bash environment using specific files. We also discuss how to incorporate bash functions to make aliases more flexible and programmatic.

Bash Alias: What Is It and How It Works

A bash alias facilitates the aggregation of several functions into one command. This way, an alias makes using a long command easy. 

So, bash aliases are maps of typically short commands that point to long commands that may involve different functions. These sets of long commands have a specific syntax and are stored in either .bashrc or .bash_profile. 

What’s nice about aliases is that you have the flexibility to combine the aliases you set with bash syntax and semantics to modify their functions further.

How It Works

The aliases you define in ~/.bashrc or ~/.bash_profile are loaded in the shell environment. So, all the commands listed mapped by the aliases are also loaded in the shell environment and can be executed at any time. 

Put simply, aliases work closely with the bash configuration to replace long commands with a string you set, called the alias keyword. You can use any and all aliases after they have been loaded into the shell. 

The interpreter replaces an alias with the corresponding command(s) mapped to it.

How To Create Aliases

Before you begin, ensure you have sudo privileges on your Linux machine.

To create an alias in bash, you must enter three things:

  1. The “alias” keyword
  1. The string you want to use as an alias
  1. The commands that must run when the alias runs

So, the syntax looks like this:

alias string=”sets of commands/functions”

Like any other Linux command, you can use options with this command. You can put the options right after the alias keyword in the syntax. 

However, there is only one flag that you can use with this option, which is the -p flag. We discuss this flag and also the unalias command at the end of this guide.

The idea of an alias is that bash must be able to run the commands you write inside them. 

You can create a temporary alias by running the alias command, like so:

alias c=’clear’

The alias above points to the clear command. So, the clear command will run when you run “c” in your shell. 

Interestingly, you also can create aliases that run scripts. All you need to do is provide the path to the script as the value of the alias. Here is what this looks like:

alias renameScript=’Example/Test/’

So, if you run the renameScript alias in your shell after running the above statement, bash will run the file_rename script.

However, once you close the shell or launch another shell, both the aliases discussed above won’t work. 

Here are the steps to creating aliases that work permanently, even after rebooting the machine.

Step #1: Create the bash_profile or bashrc File

You must begin by creating a file called bashrc or bash_profile. You must put this file in the root directory and ensure it is hidden and secure, so it is never mishandled.

You will create an alias in this file along with the commands you choose to map to it. 

To navigate to your root directory on macOS or Linux, launch a terminal and run the following command:

cd ~

You can use the command in PowerShell on Windows for the same effect. The command will also work on a git bash terminal.

Now, you must create the necessary file in the root directory. To do this on any operating system, you can run the following command:

echo >> .bashrc


echo >> .bash_profile

If the file already exists and you run this command, it won’t have any effect. However, the file will be created if there is no existing file.

You can choose to create the file in any other directory on your machine and move it to your root directory. To create the file without changing  your current directory, you can run the following command: 

echo >> ~/.bash_profile

Bear in mind that the file you need to create depends on the shell you are using.  If you are using the zsh shell, you must create the zshrc file. On the other hand, if you are using a fish shell, you must create the config/fish/ file.

Step #2: Open the File

Now that you’ve created the file, you can create an independent function in it and link it to an alias keyword. 

Begin by launching a text editor on your terminal. Below, we are using the nano text editor in the bash shell.

sudo nano ~/.bashrc

Running this command will output a huge list. Scroll down and find the section listing the default system aliases. It’s best practice to create a separate section with descriptive comments before adding your aliases using the alias command.

Step #3: Write a Function with the Command(s) in It

Here is the straightforward syntax of creating a function in bash:

function exampleFunction()


   # statements


You can also define functions directly, like so:



    # statements


As you would expect, you can create functions that accept parameters using this syntax. All you need to do is write the logic.

For instance, let’s write a function that creates a directory and navigate to this newly created directory.

Here is what the logic inside the function block would look like:

function exampleFunction()


   mkdir $1

   cd $1


The mkdir command makes the directory, and the cd command navigates inside the directory. The “$1” you see after the commands is a positional parameter. 

It accepts arguments from the command line before passing them to a function in the form of a variable. These arguments make the alias more dynamic and allow easier user input.

Step #4: Set Your Alias

After adding all the new aliases to the file, hit Ctrl+X and then Y. Finally, hit the Enter key to save the changes you’ve made.

If you want to begin using the aliases,  you can load the configuration file like so:

source ~/.bashrc

Close the terminal session. The aliases will load automatically from the next terminal session. 

Setting an Alias: Examples

Let’s go back to the example we discussed earlier. Let’s say we need an alias that creates a directory and moves into it. Again, we will need to run two commands, cd and mkdir.

To create this alias with the name “newDirectory,” you can run: 

alias newDirectory=’exampleFunction(){ mkdir “$1”; cd “$1”; }; exampleFunction’

The statement would make more sense if it were spread over several lines. However, if you look closely, you will see that we have named the alias “newDirectory.” Further, this alias is mapped to the function “exampleFunction.”

This function creates a directory named after the value passed to the “newDirectory” command before navigating into it. Note how exampleFunction is called in the end to keep the statement straightforward.

As mentioned earlier,  you can spread the same command over several lines, like so:

alias newDirectory=’exampleFunction’


mkdir $1

cd $1 


You can further customize the aliases you create by adding conditions, loops, and other programming approaches into the mix. You have full flexibility to make your alias as readable and powerful as you please.

Setting a Command with Flags as an Alias

The ls command is one of the most popular Linux commands,  and many users use it with the -la flags. These lines indicate to the command that all the files and directories must be printed, including the hidden ones, as a long list. 

Let’s create a bash alias to use this command with the -la flags. Here is a simple way to do this:

alias ll=”ls -la”

After running this in your shell, you will get the same output as using the ls command with the -la flags whenever you run “ll.”

As you can guess, this alias is temporary and won’t work when you launch the terminal the next time. However, you can make this alias persistent by adding it to the bashrc or bash_profile  file in your root directory. 

Here’s a quick look through the steps:

  1. Open the file in your root directory using a text editor like nano:
nano ~/.bashrc
  1. Type in your alias, like so:
alias ll=”ls -la”

Note that you can put a comment indicating the function of the alias by placing your comment text between a pair of “#” signs. This can help you keep track of the functions of your aliases. 

Save the file and close it. You can begin using the aliases from the next terminal launch. You can also make them available in your current terminal session by running the command below:

source ~/.bashrc

Single and Double Quote Alias Statements

An alias statement may have single or double quotes associated with it, each of which must be used to meet specific requirements.  

You can use double quotes to increase the value of a bash variable by parsing the value of the variable name. On the other hand, if you use single quotes, you won’t see the variable’s value. Just the variable name will be parsed as it is. 

Let’s see how this works in action:

#!/bin/usr/env bash


echo “The value of t is $t”

echo ‘The value of t is $t’

The output of the first echo statement with the double quotes will show be: 

The value of t is $t

However, you will see that the echo statement using single quotes will print the text in it as is. 

Bear in mind that you will also need to use double quotes to increase a variable’s value when you work with an alias in the bash_profile or bashrc file.

Other Ways to Use the alias Command in Linux

There are a few other things you can do with the alias command in Linux. For one, you can use it to display all the aliases that are currently set and active. 

To do this, all you have to do is run the alias command without any parameters or options.

To display the output of this command, which is a list of aliases, in a format that  you can input into the shell,  you can use the -p  flag like so: 

alias -p

It is also possible to delete an alias with the unalias command. Its syntax is:

unalias [name]

For instance, if you want to remove the newDirectory alias we set earlier in this tutorial,  you do not need to navigate to your root directory and remove it from the file. You can simply run the command below and it will be done:

unalias newDirectory

You can also remove all the aliases that you have set by using the -a flag like so:

unalias -a


With this tutorial handy, you now know how to use the alias command to create aliases. We have also covered how to manage aliases on your Linux machine. The command will help you streamline your workload and make quick work of large tasks.