BASH Programming

How to Create Bash Alias with Arguments and Parameters

In a BASH environment, we construct an alias for a set of files. Alias can be made more programmatic and versatile by using BASH functions, variables, etc. Bash Alias is a method for creating a few shortcut commands for numerous and repetitive operations. Here, we will discuss a way to create the bash alias with the arguments and parameters. Unfortunately, there are some cases when the alias does not accept parameters or arguments. However, we can utilize functions to accept parameters and arguments while executing alias commands. We use the bash aliases and functions to use the command line more efficiently.

Creating the Bash Alias

We already have some predefined aliases. By giving the alias command without any parameter displayed all the aliases that are already set on our system. We have opened the terminal below and specified it with the following command:

$ alias

The configured alias in our system is fetched out after running this specific command. Notice that there is an alert alias at the top of the listing. Then, a couple of aliases are available for the grep command family that outputs color information. After that, we have various alias for the “ls” command. If the custom alias is created, it will also appear in the list.

We have configured the alias of our system. Now, we have to create a custom alias which is very simple in Linux. By using the structure provided below, we can define the aliases in the command line.

$ alias MyTest='ls -l'

The alias keyword is used to declare the new alias name. As in the above alias command, we have named the alias “test”. Then, we specified the command to alias observed by the equal to sign. The command to alias here is ‘ls -l’ which lists all the directories and files in our system including the hidden ones.

Bash Alias Accepting the Arguments

We have created the alias “MyTest” with the alias structure. Now, we are going to pass an argument to that alias. By default, an alias can accept the arguments. The below command is passing the argument to the above-created alias “MyTest”. The argument here is the current directory “/”.

$ MyTest /

After just creating the alias on the terminal, we executed the alias accepting argument command which listed all the directories associated with our alias.

The alias can also accept multiple arguments at a time. Let us have the following command where we have provided the “home” directory and the “root” directory as an argument to the “MyTest” alias.

$ MyTest /home /root

The output of the above alias accepting multiple arguments displayed the results in the terminal.

Bash Alias Accepting the Parameters

Sometimes, we encounter a situation where the alias does not accept more than one argument and parameter. For this purpose, we used the bash functions to accept the various arguments and parameters. Here we are employing a basic bash function that will establish a directory and allow users to enter it without using the mkdir commands.

Newmkcd ()
{
mkdir -p -- "$1" && cd -p -- "$1"
}

The “Newmkcd” is the name of the bash function inside which we have passed the parameters “$1” and “$2”. We can insert more arguments according to our choice. It depends on where the parameter is placed after the bash function name. Note that the reserved bash function name will be stored in the $0 variable. The AND operator “&&” is utilized between these parameters to verify that if the first parameter is carried out successfully, only the second parameter is executed. Further, this dash “–” ensures no more parameters are in the function.  Now that we have created the bash function, the new directory can easily be made and moved into that directory.

Bash Alias Accepting the Parameters in the .bashrc

File

Bash shell functions can be defined in the “.bashrc” file, however, it is frequently better to place them in their own definitions file. The. bashrc file is found in the home directory of our system. When an interactive shell is opened, this file is read, and its arguments are carried out. We have to open the terminal and paste the below command.

$ gedit .bashrc

When the above command is run in the shell, it starts the “.bashrc” file on the system. There, we can see the different alias is already defined. We have to scroll down the file until the following section is reached.

All we have to do is copy that “bash_aliases” section by pressing the Ctrl+C and then paste that copied section into the same “.bashrc” file. Next, we have assigned a new name “bash_MyFunctions” to “bash_aliases” as shown in the following script snap. It then saves these changes and closes the “.bashrc” file.

Further, we created the bash_MyFunction file by using the touch command in the terminal to edit it and add a function definition.

$ touch .bash_MyFunctions

The .bash_MyFunctions file is created here. Now, we have launched it by specifying the following command on the shell.

$ gedit .bash_functions

Inside the file, we have defined the following bash shell function which accepts the arguments and the parameters.

function up() {
   levels=$1
   while [ "$levels" -gt "0" ]; do
   cd ..
   levels=$(($levels - 1))
  done
}

Here, we have begun with the keyword “function” and set the function name “up()”.  The function up() accepts just one digit-based command-line parameter. Then, we declared the variable “levels” which is set with the first parameter “$1”. The “$1” serves as the first command line parameter and it indicates a number provided by the user. After that, we deployed the “while” condition which compares the value of the “$levels” is greater than zero or positive. The while loop further contained the “cd..” command to increase the level within the directory tree. Next, is the “levels=$(($levels-1))” command. This expression sets the value of the level that is lesser than the current value.

If the level is “0” or greater than “0”, the loop runs again. And if the “$level” value is not greater than zero and has a negative value then the loop terminates here. We have saved the file and run the command which is provided below.

$ . .bashrc

The “..bashrc” command reads the “bash_MyFunctions” file when executed in the terminal.  Now, we have given a specific point in the directory tree and then used up “2” to get back to a “higher” point.

Conclusion

The guide is on how to create a bash alias with the arguments and parameters. We have explored the way which leads us to pass the arguments and parameters to the bash alias. We began with the creation of the bash alias and then run the shortcut command to pass the single and multiple arguments in the alias. After that, we have provided another way which is the bash function to pass the arguments and parameters to the alias. We became familiar with the Bash shell’s syntax for alias creation and recognized scenarios in which we want to parameterize aliases using Bash functions.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.