Create bash alias that accepts parameters

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 alias, 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

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

Example Search Function with Parameters

Let's create a more complex function that accepts multiple parameters to search for files

$ findin() { find "$1" -name "*$2*" -type f; }
$ findin /home/user "*.txt"
/home/user/documents/notes.txt
/home/user/scripts/backup.txt

Using the Alias

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.

$ 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

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 alias by using the unalias command. The syntax is

unalias <alias_name>

For example, if we want to remove the l alias from our current bash session

unalias l

To delete defined Bash functions from the current Bash session, use the unset command

unset <function_name>

For example, we can remove the mkcd function from the current Bash session

unset mkcd

Creating a Permanent Alias

If you create an alias using the command line, it will only work within the current bash session. To make the alias available for use throughout all your bash sessions, add it to either ~/.bash_profile or ~/.bashrc.

Add aliases to the ~/.bashrc configuration file

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

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

# Functions with parameters
mkcd() { mkdir -p -- "$1" && cd -P -- "$1"; }
findin() { find "$1" -name "*$2*" -type f; }

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

Key Points

Feature Simple Alias Function with Parameters
Syntax alias name="command" name() { commands; }
Parameters No Yes ($1, $2, etc.)
Use Case Fixed commands Dynamic commands
Remove unalias name unset name

Conclusion

Bash aliases provide a simple way to create shortcuts for frequently used commands, while functions extend this capability to accept parameters. Use simple aliases for fixed commands and bash functions when you need to pass arguments. Make aliases permanent by adding them to your ~/.bashrc file.

Updated on: 2026-03-17T09:01:38+05:30

965 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements