How to Create Bash Aliases with Parameters

How to Create Bash Aliases with Parameters

Bash aliases are shortcuts that allow you to use a shorter or simpler command to represent a longer or more complex command. Bash aliases are useful when you frequently use long or complex commands and want to save time and effort by using a shorter or simpler command instead. To create a Bash alias with arguments and parameters, you can use the alias command and include variables in the alias definition.

In this article, we will explore how to create Bash aliases with arguments and parameters.

Creating a Bash Alias

You can use the `alias` command for creating aliases in your Linux system.

alias alias_name='command'

Here `alias_name` is the name of the alias, and `command` is the command that you want to alias.

For example, consider the following command:

ls -l /var | grep "^d" 

This command lists the directories in the `/var` directory in a long format and filters the output to show only the directories.

To create an alias for this command, you can use the following `alias` command:

alias lsdir='ls -l /var | grep "^d"' 

This will create an alias named `lsdir` that represents the original command. To use the alias, you can simply type `lsdir` at the command prompt, and the original command will be executed.

Creating Bash Alias with Arguments

Bash aliases do not accept arguments, but we can create a function that will accept the command line parameters. These functions can be used as aliases in your Linux system. For example, consider the following function definition:

lsdir(){ ls -l $1 | grep "^d"; } 

This alias definition creates an alias named `lsdir` that takes an argument ($1) representing the directory to list. To use this alias, you can pass the directory as an argument when you invoke the alias. For example:

lsdir /var 

This will list the directories in the `/etc` directory in long format and filter the output to show only the directories.

How to Create Bash Aliases with Parameters
Creating a Bash Alias with Parameters

Setup Permanent Bash Aliases

To make the alias permanent, you can add the alias command to your `~/.bashrc` file. This will ensure that the alias is available every time you start a new Bash session.

vim ~/.bashrc 

Append the following script at end of the script.

lsdir(){
        ls -l $1 | grep "^d";
}
How to Create Bash Aliases with Parameters
Create Permanent Bash Alias with Parameters

Then source the `~/.bashrc` configuration to update the current shell environments.

source ~/.bashrc 

It is important to note that Bash aliases are not the same as Linux commands, and they are not recognized by other programs or shells. If you want to use the alias in a script or in another shell, you will need to define the alias in that script or shell as well.

Conclusion

In conclusion, Bash aliases are useful when you frequently use long or complex commands and want to save time and effort by using a shorter or simpler command instead. To create a Bash alias with arguments and parameters, you can use the alias command and include variables in the alias definition. This allows you to pass arguments and parameters to the alias when you invoke it. To make the alias permanent, you can add the alias command to your ~/.bashrc file. However, it is important to note that Bash aliases are not the same as Linux commands, and they are not recognized by other programs or shells. If you want to use the alias in a script or in another shell, you will need to define the alias in that script or shell as well.

The post How to Create Bash Aliases with Parameters appeared first on TecAdmin.

aliases bash Linux Tutorials parameters TecAdmin