Use alias command to create custom commands

by Daniel Pham
Published: Updated:
This entry is part 9 of 9 in the series Linux commands: Part 5 - Working with commands

Use alias command to create custom commands. This article will show you how to create and use it in Linux.

In the article about the types of commands available in Linux, I mentioned the alias command. Now I will say more about it.

The purpose of the alias command in Linux

There is a fact that takes place in Linux system administration, there are very long commands that we have to execute. And even, we may have to execute it many times every day.

You will be tired of repeating those long commands. And the alias command solves that problem.

So what does it do, it simply creates a custom command that represents your long command line. And instead of having to type the long command, you have a custom command.

Use alias in Linux

In the article I mentioned above, a command I have to repeat many times when working with InfluxDB is the login to its command window.

influx -ssl -unsafeSsl -host 192.168.10.10

And now, I want a more concise command to type instead of the long command above. For example, I want the custom command to be login_influx.

Recommended Reading: Use “type” command to display a command’s type

Use alias command to create custom commands
Use alias command to create custom command in Linux.

Create temporary alias

To create temporary alias, we use the command structure below.

alias custom_command='string'

With:

  • custom_command: the name of the custom command you want to use.
  • string: Long string of commands that you need to use alias instead. In this example, the main string is influx -ssl -unsafeSsl -host 192.168.10.10

Now type the following command to create alias.

alias login_influx='influx -ssl -unsafeSsl -host 192.168.10.10'

Now you can type login_influx to see if it is available.

Why is it temporary when you create alias as above? Because this alias command is only loading in the current terminal session. If you exit the terminal session that is working, this alias will be deleted.

Create alias permanently

To fix the temporary alias, we will create alias permanently. Whenever you login to the terminal, alias will be there for you to use.

Now open the ~/.bashrc file and add the above alias command to the end of the file.

Or you can run the following command to quickly add the file to the end.

echo "alias login_influx='influx -ssl -unsafeSsl -host 192.168.10.10'" >> ~/.bashrc

This file works to run alias for your session.

And then, you run this command to reload the ~/.bashrc file immediately.

source ~/.bashrc

Now try typing the login_influx command on the terminal.

login_influx

The commands work with alias

List all available alias commands

$ alias

Delete an alias of a certain command. With example above.

$ unalias login_influx

Delete all alias commands.

$ unalias -a

Conclusion

The alias command can be said to be one of the most useful commands in Linux. I myself use it very often, and believe me, you will be the same.

Hopefully with the above article, you can save time during your work. Help it less boring.

(This is an article from my old blog that has been inactive for a long time, I don’t want to throw it away so I will keep it and hope it helps someone).

0 0 votes
Article Rating
Series Navigation«« Previous part: Display program’s info entry with info command

You may also like

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

DevOps Lite is a personal blog specializing in technology with main topics about DevOps, DevSecOps, SRE and System Administrator. Articles are shared for free and contributed to the community.

SUPPORT US

FOLLOW US

Subscribe my Newsletter for new blog posts. Stay updated from your inbox!

© 2021-2024 DevOpsLite.com – All rights reserved.

Please write sources “DevOpsLite.com” when using articles from this website.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

2
0
Would love your thoughts, please comment.x
()
x

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.