1. Overview

While working with Bash scripts or Linux commands, we often end up writing the same commands again and again. Many times, these commands are long and need to be executed repeatedly – for example, logging on to a remote machine every day, copying a local file to the remote machine, or looking for hidden files or links in a directory.

We can use the alias command to replace the long monotonous shell command with another, much shorter name. In this tutorial, we’ll explain the steps to create an alias and learn how to pass parameters to it in the Bash shell.

2. Creating a Bash alias

The alias command helps to create an alternate name that we can substitute for complex Linux commands and scripts. The syntax to create an alias is simple:

alias <alias_name>="<command to run>"

For instance, we can create an alias ‘l‘ listing all the files and directories in the current folder:

alias l="ls -alrt"

3. Creating an alias with Parameters

Sometimes we need to create an alias that accepts parameters. Since the alias command created in Bash does not accept parameters directly, we’ll have to create a Bash function. The syntax of the Bash function is:

<function_name> {
<commands>
}

OR

function <function_name> {
<commands>
}

While defining the function, we use $1, $2, and so on, as variables to identify the parameters passed to the function. $0 is a reserved variable used for the function name. Here’s an example of a function named mkcd:

~soft $ mkcd() { mkdir -p -- "$1" && cd -P -- "$1"; } 
~soft $ pwd 
/var/home/user/soft 
~soft $ mkcd newdir 
~newdir $ pwd 
/var/home/user/soft/newdir

Note: An alias command just substitutes one text with another in the Bash shell. Consider the example of the alias ‘l‘ defined above. Let’s use ‘l‘ to list files available in some path in our filesystem:

[~user ]$ l Pictures/ 
total 308 
drwx------. 1 user user 504 May 25 20:33 .. 
drwxrwxr-x. 1 user user 42 Jun 4 13:02 old 
drwxr-xr-x. 1 user user 312 Jun 4 13:02 new 
-rw-r--r--. 1 user user 154716 Jun 4 13:03 a.png 
drwxr-xr-x. 1 user user 168 Jun 8 09:43 . 
-rw-r--r--. 1 user user 156060 Jun 8 09:43 b.png

Please mind that in the above example, ‘l’ substitutes ‘*ls -alrt*‘. Since we pass the file path immediately after the alias, we do not need to use a function in this case.

4. Removing an alias

We can remove the defined alias from the Bash session using the unalias command. The syntax of the unalias command is pretty trivial:

unalias <alias_name>

For example, let’s remove the ‘l‘ alias from the current Bash session:

unalias l

Similarly, we’ll use the unset command to remove the defined Bash function from the current Bash session. The syntax of the unset command is also unremarkable:

unset <function_name>

For example, we can remove the ‘mkcd’ function from the current Bash session by typing:

unset mkcd

5. Creating a Permanent alias

When created from the command line, the alias works only in the current Bash session. To make the alias permanent and available across all our Bash sessions, we need to declare it in the ~/.bash_profile or ~/.bashrc file.

We’ll add the alias in the ~/.bashrc file:

# Aliases
# alias alias_name="<command to run>"

# Long format list
alias l="ls -alrt"

Note: The alias names should be kept short and easy to remember. It is recommended we add comments to them for future reference.

6. Conclusion

In this tutorial, we learned about the alias command, including why and when we should create aliases. We learned the syntax for creating an alias in the Bash shell and identified situations when we might want to use Bash functions to parameterize aliases. Finally, we discussed how to permanently create an alias and how can we remove it from the current Bash session.