Different Ways to Create and Use Bash Aliases in Linux

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.

$ alias
Check List of Defined Linux Aliases
Check List of Defined Linux Aliases

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
Create Bash Aliases File
Create Bash Aliases 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
Bashrc Alias Definition
Bashrc Alias Definition

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$//'\'')"'
Bashrc Defined Aliases
Bashrc Defined Aliases

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
Create an Alias in Linux
Create an Alias in Linux

To make the alias persistent, add it to the .bash_aliases file. You can use your favorite text editor or use the cat command or echo command to add an alias.

$ echo alias nf="neofetch" >> ~/.bash_aliases
$ cat >> ~/.bash_aliases
$ cat ~/.bash_aliases
List Defined Aliases
List Defined 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.

$ nf
Run Alias in Linux
Run Alias in Linux

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
Override Default Behaviour of Command
Override Default Behaviour of Command

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
Unchanged Command Behaviour
Unchanged Command Behaviour

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
Remove an Alias in Linux
Remove an Alias in Linux
NOTE: Unalias will remove the alias definition from the currently loaded session, not from .bashrc or .bash_aliases.

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
Global Profile File
Global Profile File
NOTE: It is best practice to take a backup of user-level or system-level files. If in case something went wrong backup copy can be reverted.

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.

Hey TecMint readers,

Exciting news! Every month, our top blog commenters will have the chance to win fantastic rewards, like free Linux eBooks such as RHCE, RHCSA, LFCS, Learn Linux, and Awk, each worth $20!

Learn more about the contest and stand a chance to win by sharing your thoughts below!

Karthick
A passionate software engineer who loves to explore new technologies. He is a public speaker and loves writing about technology, especially about Linux and open source.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

3 Comments

Leave a Reply

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.