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 the alias command in Linux that allow us to create shortcuts for custom commands.
Creating an Alias
Let’s take a look at the syntax for creating an alias on a linux system using the command line:
alias name="command"
Every alias we make will follow this format, we use the alias
keyword, give our alias a name, and inside the quotation marks we will put the command/script/service we want our alias to represent.
Note: This is how you make a temporary alias, 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, what would take a couple of commands in linux to update a system can be made into 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"
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, the same can be applied to bashrc file as well:
# 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 create aliases in this 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
source
.Using Aliases
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
“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!