Create bash alias that accepts parameters


Overview

When working with Bash scripts or Unix/Linux command line tools, we often write the same command lines over and over again. Often, these command lines are long and must be repeated multiple times. For instance, when logging into a remote server daily, copying a local folder to the remote server, or searching for hidden files or directories within a directory.

You can create aliases using the alias command. In this guide, I will show you how to create an alias that accepts parameters on Linux. This is useful if you want to run a single command repeatedly without having to type it out each time.

Creating a Bash alias

Aliases help us to create alternative names for complex Linux commands and shell scripts. To create an Aliais, simply use the following syntax −

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

For example, if we want to list all the files and folders in our current directory, we could use an alias called l.

alias l="ls -alrt"

Creating an alias with Parameters

We sometimes want to create aliases that accept parameters. Since the alias commands don't 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>
}

We define functions by using $1, $2,...$n as variables to identify the arguments passed to the functions. $0 is a special variable which identifies the names of the functions themselves. 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 − A bash alias is a way to substitute one text for another within the Bash shell. For example, consider the alias l which was previously described. We’ll use ‘l’ to list files available in a certain directory in our file system.

[~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

If you want to see what files are currently open, use ls −alrt instead of l. Since we're passing the file path directly after the alias, we don't require any functions here.

Removing an alias

You can remove an existing definition for a shell variable by using the unset command. The syntax of the alias command is pretty simple −

unalias <alias_name>
For example, if we want to remove the 'l' alias from our current bash session, we could use the following command:
unalias l

We’ll use the reset command to delete the defined Bash functions from the current Bash session (if any). The syntax of the UNSET command is also quite simple −

unset <function_name>

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

unset mkcd

Creating a Permanent alias

If you create an alias using the command line, it will only work within the current bash session. We want to make the alias available for use throughout all our bash sessions, so we'll add it to either ~/.bash_profile or ~/.bashrc.

We'll add an alias for the command in the ~/.bashrc configuration files −

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

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

Keep the alias names short and easy to remember! We recommend adding notes to each one so they're easier to refer back to later.

Conclusion

We learned about the alias (or short for "alias") commands, including why and when to use them. We've seen the syntax for defining an alias in the Bash Shell and identified some situations where we might want to use bash function to define them. We discussed how to permanently create a new alias and how to delete it from the current bash session.

Updated on: 23-Dec-2022

579 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements