aliases – TecAdmin https://tecadmin.net How to guide for System Administrator's and Developers Tue, 03 Jan 2023 08:46:04 +0000 en-US hourly 1 https://wordpress.org/?v=6.1.1 How to Create Bash Aliases with Parameters https://tecadmin.net/how-to-create-bash-aliases-with-parameters/ https://tecadmin.net/how-to-create-bash-aliases-with-parameters/#respond Tue, 03 Jan 2023 08:34:12 +0000 https://tecadmin.net/?p=33217 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 [...]

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

]]>
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.

]]>
https://tecadmin.net/how-to-create-bash-aliases-with-parameters/feed/ 0
What is the /etc/aliases file https://tecadmin.net/what-is-the-etc-aliases-file/ https://tecadmin.net/what-is-the-etc-aliases-file/#respond Wed, 29 Jun 2022 03:53:08 +0000 https://tecadmin.net/?p=30396 What is /etc/aliases? /etc/aliases is a text file used to store email aliases on a Linux system. Email aliases are basically nicknames for email addresses. They allow you to send emails to a group of people using a single address, or to redirect emails from one address to another. /etc/aliases are typically used to store [...]

The post What is the /etc/aliases file appeared first on TecAdmin.

]]>
What is /etc/aliases?

/etc/aliases is a text file used to store email aliases on a Linux system. Email aliases are basically nicknames for email addresses. They allow you to send emails to a group of people using a single address, or to redirect emails from one address to another.

/etc/aliases are typically used to store aliases for the system’s mail server. However, it can also be used to store aliases for any other purpose.

For example, you could use /etc/aliases to create an alias for your own email address. This file is stored in the /etc directory, which is the standard location for system-wide configuration files. /etc/aliases are usually managed by the system administrator. However, you can also edit /etc/aliases yourself if you need to add or change an alias.

Adding entry to /etc/aliases

To add a new alias to /etc/aliases, simply open the file in a text editor and add a new line with the following format:

alias_name: real_name@domain.com

Replace the alias_name with the desired alias, and real_name@domain.com with the actual email address you wish to forward messages to. You can also forward one user’s email to another user.

Once you have saved your changes, run the newaliases command to update the aliases database.

newaliases

Your new alias will now be active and forward messages as expected.

Hope this tutorial helps you to understand the uses of /etc/aliases file.

The post What is the /etc/aliases file appeared first on TecAdmin.

]]>
https://tecadmin.net/what-is-the-etc-aliases-file/feed/ 0