Use alias command to create custom commands

Share this post to your friends !

This entry is part 9 of 9 in the series Linux commands: Part 5 - Working with commands
Use alias command to create custom commands
5 (100%) 2 vote[s]

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

use-alias-to-create-custom-commands 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.

Continue reading the series«« Previous part: Display program’s info entry with info command
If you liked this article, then please subscribe to our YouTube Channel for more video tutorials. You can also find us on Twitter and Facebook.


Share this post to your friends !
If you appreciate what we share in this blog, you can support us by:
  1. Stay connected to: Facebook | Twitter | Google Plus | YouTube
  2. Subscribe email to recieve new posts from us: Sign up now.
  3. Start your own blog with SSD VPS - Free Let's Encrypt SSL ($2.5/month).
  4. Become a Supporter - Make a contribution via PayPal.
  5. Support us by purchasing Ribbon Lite Child theme being using on this website.

We are thankful for your support.

single post bottom banner
«« »»

Got something to say? Join the discussion

Please keep in mind that all comments are subject to our Comment Policy. Your email address will not be published.
This site uses Akismet to reduce spam. Learn how your comment data is processed.