An alias is a shortcut command generally used to reference another command or to execute a command in a preferred way. It can be used as an efficiency tool to avoid typing in long commands or to correct command input.
As an example, on systems which use the package manager Apt, the following two commands need to be executed in order to list available updates:
sudo apt update && apt list --upgradable
To avoid having to remember these commands - or type all that in each time - an alias can be used.
How to create an alias
One way to setup a new alias quickly is to add a new one from the shell.
NOTE: this method only activates the alias for the current shell session and will be lost as soon as the shell is closed. To make an alias permanent, a configuration file must be used - skip ahead.
following example, we will make a new alias,
cu, short for Check
alias cu="sudo apt update && apt list --upgradable"
When you next type in
cu the underlying command will execute.
To remove an alias, use the
unalias command, specifying which alias to remove:
A list of all aliases can be viewed by typing in
alias into the shell.
$ alias # output alias clean='sudo apt autoremove' alias cu='sudo apt update -qq && apt list --upgradable' alias egrep='egrep --color=auto' alias fgrep='fgrep --color=auto' alias grep='grep --color=auto' alias in='sudo apt install' alias l='ls -CF' alias la='ls -A' alias list='apt list available' alias ll='ls -alF' alias ls='ls --color=auto' alias python='python3' alias remove='sudo apt purge'
Bash and Zsh configuration files
.zshrc files can be used to set configuration for Bash and Zsh
respectively. The file is located in the user’s home directory and is hidden.
To locate it, use the command
ls -a. If you don’t have one, create it by
touch ~/.bashrc #OR... touch ~/.zshrc
Using the previous example above, open the file with a text editor and add a new line:
alias cu="sudo apt update && apt list upgradable"
Save and close, then run the following to load the changes:
All the newly added aliases will become persistent until removed.
Bash aliases file
.bash_aliases file is a user-specific file containing aliases. It is not
essential to have user aliases separated from the main
.bashrc file but it
can be useful for situations where, for example, aliases need to be moved
Similar to the
.bashrc file, create the file if it does not exist. Add one
line per alias to the file, save and close, then run the following to load the
IMPORTANT: If using a
.bash_aliases file, ensure that
contains the following which points to your alias file:
if [ -f ~/.bash_aliases ]; then . ~/.bash_aliases fi
Some alias examples
Below are some examples of using aliases.
alias python="python3" alias backup="~/Documents/backup.sh" alias clean="sudo apt autoremove" #check for updates quietly alias cu="sudo echo Checking for updates... && sudo apt update -qq && apt list upgradable" #Install - e.g. "in gthumb" will install gthumb alias in="sudo apt install" #List - e.g. "list gthumb" will list available programs named gthumb alias list="apt list available" alias remove="sudo apt purge" alias upgrade="sudo apt update -qq && sudo apt upgrade"