Tired of typing the same commands over and over? A Bash alias is a method of supplementing or overriding Bash commands with new ones. You can save yourself some time by creating aliases for your most used commands.
Table of Contents
As system administrators we inevitably find ourselves using the same commands over and over.
What is an Alias in Linux?
Well, an alias is a custom command created by the user to execute another, usually more complicated command or group of commands. Therefore, it is a shortcut to reference a command. In other words, alias is shortcut command which will have same functionality as if we are writing the whole command.
For example, you could set the alias
tgz to be a shortcut for the
tar -xvzf command.
You’ve found it very helpful to create aliases to make your command line life easier.
List Currently Defined Linux Aliases
You can see a list of defined aliases on your profile by simply executing
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"' alias egrep='egrep --color=auto' alias fgrep='fgrep --color=auto' alias grep='grep --color=auto' alias l='ls -CF' alias la='ls -A' alias ll='ls -alF' alias ls='ls --color=auto'
Above you can see the default aliases defined for your user in Ubuntu 20.04. As you can see, executing
ll is equivalent to running
Using aliases like this you can create anything from simple shortcuts like this to powerful custom commands.
How to Create a Linux Command Alias
Creating a Linux alias is very easy. The syntax is as follows:
- Start with the
- Then type the name of the alias you want to create
- Add an
=sign, with no spaces on either side of the
- Then type the command (or commands) you want your alias to execute when it is run. This can be a simple command, or can be a powerful combination of commands.
You can either enter them at the command line as you’re working, or more likely, you’ll put them in one of your startup files, like your
.bashrc file, so they will be available every time you log in.
An alias in Linux can be directly set in the shell as follows:
alias ll='ls -alF'
Now when the
ll command is run it will use the alias and the
Note that setting an alias in Linux in this way only works for the life of a shell session. When the shell is closed the alias will be lost. To make an alias persist across shell sessions and reboots a configuration file for the shell should be used. For bash this is the
The .bashrc file is located in your home directory. Open the file in your text editor:
Now find a place in the file, where you want to keep the aliases. For example, you can add them in the end of the file:
# Aliases # alias alias_name="command_to_run" # Long format list alias ll="ls -la" # Print my public IP alias myip='curl ipinfo.io/ip'
Save the file. Now, reload the
.bashrc file using the following command:
While this Linux alias is a simple quality of life change, you can apply this same concept to long and complicated commands that you might have to use routinely during your daily work. So, now you can think about the commands you use the most and create shortcuts for them in your shell.
For more about
alias command in Linux, consult its manual page.