How to Create Alias for Two Commands on Linux

How to Create Alias for Two Commands on Linux

Figuring out how to create alias for two commands on Linux is a quick and easy way to make writing code faster. Here’s everything you need to know about it. 

If you’re a Linux user, you often get stuck when you need to write a series of long and complicated commands. And that stuff can be quite the ordeal. In Linux, the alias is used to provide a short name for commands. Along with the commands, some parameters and options that can also be associated with them. 

Generally, we provide an alias for a single command. However, we can even specify an alias for two or more commands. In this guide, we will discuss how to create an alias for commands in Linux. In order to execute the commands, you will need a computer equipped with the Linux operating system. 

How to Create Alias for Two Commands on Linux

An alias is a short name or shortcut for a single command or a set of commands in Linux. It replaces itself with the associated commands and invokes the ones associated with it. The main motivation for using an alias in Linux is to replace long and complicated commands, improve user efficiency, and avoid potential spelling mistakes. 

In addition, you can even provide options with alias making it much more easier to use commands in Linux.

Usage of command

The general usage of this command is as follows:

alias [option] [name]='[value]'

Here, you can provide the name of the alias followed by the list of commands as values. 

Creating an alias in Linux

To create an alias for listing the current home directory, you can use the following command:

$alias lh = ls /home/khan

The above command will create an alias that will list down the directory for current user khan.

Listing down aliases

By default, when you run the alias command only, it displays the list of alias on the terminal. The following command shows the list of alias created in your system:

$alias

You can also provide the –p option to display the list of alias in a format suitable for input to the shell. The following command illustrates how you can list alias using the –p option:

$alias -p

Creating an alias for two commands

The alias command can be used to provide an alias for commands. The commands are very similar to the bash script. You just need to provide the commands delimited by semicolons. For instance, the following command creates an alias for two list commands:

$alias list_dev_hom = "ls /dev; ls /home"

In the command above, we have listed down the contents of /dev and /home using the ls command.

Creating an alias for two different commands

In the previous example, we have used two commands of the same type. However, we can even use two different commands as follows:

$alias write_file ="touch myfile.txt; echo > myfile.txt created"

The above command first creates a file named myfile and then echoes a message “created” to be written to the file.

Creating an alias for multiple commands

We can even create an alias for multiple commands (i.e., more than two commands). You just have to repeat the process and provide the commands delimited by semicolons. Inputting the command below creates a file myfile.txt, writes a line to the file, and then shows its contents to the console:

$alias write_file = "touch myfile.txt; echo > myfile.txt created; cat myfile.txt"

Storing alias permanently using .bashrc

There are two types of alias in Linux: temporary and permanent. Temporary alias can be created via terminals. However, to create permanent aliases, you need to store them in .bashrc files. We will now discuss whether you can store the alias permanently using the .bashrc file.

Editing .bashrc

You can provide an alias for your command in a terminal. However, this will remain only for the current session. As an alternative, you can provide the alias in your bashrc file. Just open your favorite editor, such as using the nano editor, by typing the following command:

$nano ~/.bashrc

Provide an alias for locking the screen saver

Now, you can provide the alias for your commands. For instance, you can provide an alias for locking your screen saver by typing the command below:

alias lock='gnome-screensave; gnome-screensaver-command --lock'

Using an alias where the second command is dependent on the first command

If your second command is dependent on the first command, then you can use the short circuit, evaluation operator (&&). The following command illustrates it:

alias loc='gnome-screensaver && gnome-screensaver-command --lock'

In the line above, the second command will not be attempted if the first command doesn’t run successfully.

Using arguments to lock screen saver

Alias is generally used for renaming commands. If you work beyond that, you should use functions. So, to do that, you’ll have to use arguments in the previous scenario. You can also write a function in the .bashrc file as follows:

lock() {

 gnome-screensaver

 gnome-screensaver-command --lock

}

Tip: As a shortcut to editing the .bashrc file, you can make the alias permanent by using the following command:

$ echo "alias lh='ls ~" >> ~/.bashrc

In the previous command, the alias command is echoed to the .bashrc file.

Removing alias

To remove an alias, you can use the unalias command. For instance, to remove the alias lock, you can use the following command:

$unalias lock

This will remove the lock alias from the system. You can also remove all the alias from the system by using the following command (with the –a option):

$unalias -a

We have discussed the alias command and its various options—creating an alias for single or multiple commands, and how to set an alias for more than one command. And finally, we also tackled the process needed to remove the alias from the system. 

If you wish to know more, additional information about the command can be seen in the manual.

If this guide helped you, please share it.

Leave a Reply
Related Posts