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:
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.
Removing an Alias
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.
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"
updateinstead of all that extra malarkey to update a system.
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
~/.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.
aliasyou defined and it will perform whatever commands it was assigned to. Your shortcut should work and you’ll be off at the races!
“zsh: command not found:”double check your aliases and reload your
.bashrcfile 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"
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!