Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How To Use Aliases in Linux with ALIAS and UNALIAS

As Linux users we spend a lot of time in our terminals getting things done; updating our systems, installing packages, development, etc. We live in our terminals, and as such we often end up inputting the same commands over and over again. Running sudo apt update everyday gets really old, really quick. Thankfully, as Linux users we have aliases that allow us to create very handy shortcuts for frequently used commands or long complex ones.

Creating an Alias

Let’s take a look at the syntax for creating an alias:
				
					alias name="command"
			
Every alias we make will follow this format, we use the alias keyword, give our alias a name, and inside the double quotes we will put the command(s) we want our alias to represent.

Note: aliases defined in the terminal are only available in the current session and are lost on subsequent shell sessions.

Removing an Alias

The unalias command is just a way to remove previously defined aliases that we might not need anymore for whatever reason. Simply use unalias to remove any previously defined ones.
				
					unalias update
			

A practical example

For example, one alias that I always make on any system is updating the system in one command. For an Arch distro configured with the yay AUR package helper, I would do something like this:

				
					alias update="yay -Syu --noconfirm"
			

In one command I can update and upgrade both my core and AUR packages in one go without having to manually input anything. Here’s the same command but for a Ubuntu system:

				
					alias update="sudo apt update && sudo apt upgrade -y"
			
After running this command, you can simply type update instead of all that extra malarkey to update a system.

Note: aliases are usually defined in the shell config file. This allows you to define and store your aliases for future shell sessions. More details below:

Storing aliases (using bash or zsh)

So it would be pretty wack to make all these sick shortcuts and then have to constantly re-enter them when we wanted to use them. Luckily, we don’t have to do that because your shell comes with a config file ( typically located in the root of your home directory), common examples include bash (~/.bashrc) or zsh (~/.zshrc). These config files provide a wealth of configuration options for your shell and often come with an area that is predefined for aliases. Here’s a snippet of my .zshrc alias section on Ubuntu:

				
					# Set personal aliases, overriding those provided by oh-my-zsh libs,
# plugins, and themes. Aliases can be placed here, though oh-my-zsh
# users are encouraged to define aliases within the ZSH_CUSTOM folder.
# For a full list of active aliases, run `alias`.
#
# Example aliases
# alias zshconfig="mate ~/.zshrc"
alias update="sudo apt update && sudo apt upgrade -y"

#git
alias pull="git pull"
alias push="git push"
alias stash="git stash"
alias apply="git stash apply"
			

Simply add your aliases to the file using a text editor then reload your shell session or use the following command to reload the shell config. This file will load up whenever you start a new terminal and all your aliases will be available and ready to use.

				
					source ~/.bashrc
			
And if you need to remove any of these aliases you can simply comment them out or just cut them out of the file and reload the file using source.

Using Aliases

Using aliases is just as simple, simply call the alias you defined and it will perform whatever commands it was assigned to. Your shortcut should work and you’ll be off at the races!
				
					update
			
If you get “zsh: command not found:” double check your aliases and reload your .zshrc or .bashrc file and try again. Aliases aren’t just terminal bound as they can also be used inside of scripts or used to call files, etc.
				
					alias script="sh ~/someScript.sh"
			

Conclusion

Using alias commands allow us to make custom shortcuts for any command or operation on our system and save us a lot of time and mistakes be reducing manual input. Long commands can be transmuted into much shorter and simpler ones. The usefulness of this utility goes up exponentially when you throw in bash-scripting into the mix. Aliases kick ass and if you aren’t using them on your system you are missing out. Thanks for reading and make sure to subscribe for more command coverage!



This post first appeared on Linuxman Tech, please read the originial post: here

Share the post

How To Use Aliases in Linux with ALIAS and UNALIAS

×

Subscribe to Linuxman Tech

Get updates delivered right to your inbox!

Thank you for your subscription

×