Alias in bash can be termed simply as a command or a shortcut that will run another command/program. Alias is very helpful when our command is very long and for frequently used commands. Over the course of this article, we are going to see how powerful is an alias and the different ways to set up an alias and use it.
Check Bash Aliases in Linux
Alias is a shell builtin command and you can confirm it by running:
$ type -a alias alias is a shell builtin
Before jumping and setting up an alias we will see the configuration files involved. An alias can be set either at the “user-level” or “system level”.
Invoke your shell and simply type “alias” to see the list of defined alias.
User-level aliases can be defined either in the .bashrc file or the .bash_aliases file. The .bash_aliases file is to group all your aliases into a separate file instead of putting it in the .bashrc file along with other parameters. Initially, .bash_aliases will not be available and we have to create it.
$ ls -la ~ | grep -i .bash_aliases # Check if file is available $ touch ~/.bash_aliases # Create empty alias file
Open the .bashrc file and look out for the following section. This section of code is responsible for checking if file .bash_aliases is present under the user home directory and load it whenever you initiate a new terminal session.
# Alias definitions. # You may want to put all your additions into a separate file like # ~/.bash_aliases, instead of adding them here directly. # See /usr/share/doc/bash-doc/examples in the bash-doc package. if [ -f ~/.bash_aliases ]; then . ~/.bash_aliases fi
You can also create a custom alias file under any directory and add definition in either .bashrc or .profile to load it. But I will not prefer this and I choose to stick with grouping all my alias under .bash_aliases.
You can also add aliases under the .bashrc file. Look out for the alias section under the .bashrc file where it comes with some predefined aliases.
# enable color support of ls and also add handy aliases if [ -x /usr/bin/dircolors ]; then test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)" alias ls='ls --color=auto' #alias dir='dir --color=auto' #alias vdir='vdir --color=auto' alias grep='grep --color=auto' alias fgrep='fgrep --color=auto' alias egrep='egrep --color=auto' fi # colored GCC warnings and errors #export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01' # some more ls aliases alias ll='ls -alF' alias la='ls -A' alias l='ls -CF' # Add an "alert" alias for long running commands. Use like so: # sleep 10; alert 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$//'\'')"'
Creating Alias in Linux
You can either create a temporary alias that will be stored only for your current session and will be destroyed once your current session ends or permanent alias which will be persistent.
The syntax for creating an alias in Linux.
$ alias <name-of-the-command>="command to run"
For example, in a real scenario.
$ alias Hello="echo welcome to Tecmint"
Open the terminal and create any alias command you desire. If you open another session then the newly created alias will not be available.
$ alias Hello"echo welcome to Tecmint" $ alias $ Hello
$ echo alias nf="neofetch" >> ~/.bash_aliases $ cat >> ~/.bash_aliases $ cat ~/.bash_aliases
You have to reload the .bash_aliases file for the changes to be effective in the current session.
$ source ~/.bash_aliases
Now if I run “nf” which is an alias for “neofetch” it will trigger the neofetch program.
An alias can come in handy if you wish to override the default behavior of any command. For demonstration, I will take an uptime command, that will display system uptime, the number of users logged in, and the system load average. Now I will create an alias that will override the behavior of the uptime command.
$ uptime $ cat >> ~/.bash_aliases alias uptime="echo 'I am running uptime command now'" $ source ~/.bash_aliases $ uptime
From this example, you can conclude the precedence falls to bash aliases before checking and invoking the actual command.
$ cat ~/.bash_aliases $ source ~/.bash_aliases $ uptime
Removing an Alias in Linux
Now remove the uptime entry from the .bash_aliases file and reload the .bash_aliases file which will still print the uptime with alias definition. This is because the alias definition is loaded into the current shell session and we have to either start a new session or unset the alias definition by running the unalias command as shown in the below image.
$ unalias uptime
Adding System-Wide Aliases
Till this point, we have seen how to set up an alias at the user level. To set an alias globally you can modify the “/etc/bash.bashrc” file and add aliases which will be effective globally. You need to have the elevated privilege to modify bash.bashrc file.
Alternatively, create a script under “/etc/profile.d/”. When you log in to a shell “/etc/profile” will run any script under profile.d before actually running ~/.profile. This method will reduce the risk of messing up either /etc/profile or /etc/bash.bashrc file.
$ sudo cat >> /etc/profile.d/alias.sh alias ls=”ls -ltra”
Below is the code grabbed from the /etc/profile that takes care of running any scripts that we put under /etc/profiles.d/. It will look out for any files with the
.sh extension and run the source command.
$ tail /etc/profile
That’s it for this article. We have seen what is alias, the configuration files involved with the alias, and different ways to set up the alias locally and globally.