Alias example accepting arguments and parameters on Linux

Creating an alias is a good way to make commands easier to remember and quicker to type. In case you want to extend the functionality of your aliases even further, it is possible to have them accept arguments and parameters. This gives users the ability to execute complex and lengthy commands in only a few keystrokes on the command line.

In this tutorial, we will show you how to create an alias that can accept arguments or parameters on the command line. We will give you a simple example, which you can copy and paste onto your own system, and adapt it to your own scenarios as needed.

In this tutorial you will learn:

  • How to create an alias that can accept arguments and parameters
Alias example accepting arguments and parameters on Linux
Alias example accepting arguments and parameters on Linux
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software N/A
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Alias example accepting arguments and parameters on Linux



Accepting arguments

Aliases in the Bash shell can already accept arguments by default, so there is really nothing fancy that you need to do here. Let’s look at an example.

$ alias test='ls -l'

We can now supply an argument (in this case, a directory) to the alias we created.

$ test /
Output from our example alias accepting an argument
Output from our example alias accepting an argument

You can also supply multiple arguments to the alias. For example, let’s pass two arguments.

$ test /home /root

Accepting parameters

Configuring an alias to accept parameters is a little more complicated. We will need to use a Bash function in order to facilitate this.

As an example, we will create an alias (or function) which allows us to create a directory (mkdir) and then navigate to that directory (cd) in a single command. We will call the alias mkcd, but the function also needs a (different) name, so we will call the function mkcdf.

$ alias mkcd='function mkcdf(){ mkdir "$1"; cd "$1"; }; mkcdf'

And now we can use our alias:

$ mkcd /home/linuxconfig/test




Our alias accepts a parameter, allowing us to make and cd to a directory with one command
Our alias accepts a parameter, allowing us to make and cd to a directory with one command

It really is not necessary to use an alias for parameters, since functions can do it on their own. The recommended approach here would be to forget about the alias and just use a function instead:

$ function mkcd(){ mkdir "$1"; cd "$1"; }
NOTE
Remember to edit your user’s ~/.bashrc file with the aliases and functions you want to use permanently. See our tutorial on Creating a permanent alias for help.

Closing Thoughts

In this tutorial, we saw how to create an alias that can accept arguments and parameters on a Linux system. While technically possible to achieve with aliases, we have also shown an example of how a Bash function can be used for this functionality by itself.



Comments and Discussions
Linux Forum