I was looking at some Kubernetes tutorials on Youtube and I noticed something interesting.
Kubernetes is really complex and I’m not intelligent enough for this industry.
Well, apart from that I noticed that the instructor was writing a command called k instead of the kubectl which is usually used. When I tried to reproduce the shell command locally, the terminal got mad at me – the command was not found, it said! After scratching my head for a while, I understood – to save time the instructor actually created his own local command called an alias! Amazing stuff. But how?
Multiple commands on one line
A really cool and helpful command line trick is the ability of the shell to actually understand more than one command on a single line, and the only thing we need to do is to separate them by semicolons.
cd Public; ls -la; touch file.txt; cd -
Above we changed the current directory to the Public directory, then we listed the contents of that directory, then we created a file called file.txt using the touch command, and then, finally using cd – we returned to the original directory.
The Alias Command
We can actually turn this sequence of commands into a brand new, custom command. How? By using the following command (structure below):
alias aliasName='sequence of commands'
After writing ‘alias’ as the command, we provide the alias name, and the content of commands (no spaces allowed). After the alias is defined it can be used anywhere in the shell. So, let us create an alias using the commands above.
Let us go through this line by line.
- The shell complained that something was wrong! Can you figure out what we did wrong? Yeah, of course – we had whitespace between the name of the alias and the equality sign, which is actually forbidden.
- This was fixed in the second line, we created an alias which we now can freely use in our command line.
- We called our new command (amazing!), and it printed out the contents of the public folder (a file.txt was already in there beforehand)
- We wrote another command – type, which will tell us what kind of command mktouch actually is (this can be really helpful when finding out whether the command is a shell built-in, an executable, a function, or an alias!
Removing the alias
We can simply remove alias by writing the following command:
5. With unalias commandName, we actually removed our alias.
6. The last command alias will list all the defined alias in the environment. I have a ton of them locally!
Now, write a new alias, test if it works, and then shut down the shell session. Open up another one, and… Well, you can’t use the alias anymore! This happens because the alias only lives while the terminal is open. But there is a workaround!
In your folder structure search for the following file ~/.bashrc. Just add the new alias command at the end of this file. Next time, you’ll have your new command everywhere!
Conclusion
That is it when it comes to aliases, I hope you had as much as fun reading this post I was writing it!
While you wait, you can read more Linux related posts on the Linux subsection here or click on any of the links below: