Create custom command in AlmaLinux 9

Published: December 24, 2022

Last updated: December 24, 2022

Create custom command in AlmaLinux 9

In this article, we cover how to create our own commands in AlmaLinux 9. This can be achieved through alias shell builtin command. Issue the following command in terminal to get information about pre-existing aliases in our system -

alias

In the standard output, one of the entry would show how la command corresponds to ls -A. So, if we enter ls -A or la it would result in similar outcomes. Furthermore, if there is a specific command set we use regularly. Then, it can be shortened. And, that is exactly where alias steps in. It can be used to create custom commands.

Create custom command in AlmaLinux 9

Use following syntax to create custom commands in AlmaLinux 9 -

alias command_name='command-string'

Note: Use a unique command_name.

Understand it with the help of an example. Let's say we want to create an empty file with touch command in /dev/shm directory then we can use the following two commands -

cd /dev/shm
touch test-file

Now, if we want to create a custom command - cmdtouch which can get us in the directory /dev/shm and creates a test-file inside it. Then, enter the following in terminal itself -

alias cmdtouch='cd /dev/shm; touch test-file'

Thereafter, run alias command in the terminal to check whether it shows in the list.

alias

So, the following command would get us in the directory /dev/shm and create an empty file test-file.

cmdtouch

But, the custom command wouldn't be available once we close the terminal session. To make permanent changes, append the file ~/.bashrc

nano ~/.bashrc

with the following -

alias cmdtouch='cd /dev/shm; touch test-file'

Save and exit.

And, that is pretty much everything we have to do.

In conclusion, we have covered how to make custom commands in AlmaLinux 9. If there is a specific command set which we often use then, it is better to create our own command to get things done faster.