How to check if a shell alias is defined in Linux

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to check if a shell alias is defined in Linux. You will begin by listing the currently defined aliases using the alias command to understand what aliases are and how they are displayed.

Next, you will explore common locations where aliases are configured. This includes checking user-specific aliases typically stored in the ~/.bashrc file and inspecting system-wide aliases found in /etc/bash.bashrc. By the end of this lab, you will be able to identify and locate alias definitions within your Linux environment.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("Linux")) -.-> linux/BasicFileOperationsGroup(["Basic File Operations"]) linux(("Linux")) -.-> linux/TextProcessingGroup(["Text Processing"]) linux/BasicFileOperationsGroup -.-> linux/ls("Content Listing") linux/BasicFileOperationsGroup -.-> linux/cat("File Concatenating") linux/TextProcessingGroup -.-> linux/grep("Pattern Searching") linux/TextProcessingGroup -.-> linux/sed("Stream Editing") subgraph Lab Skills linux/ls -.-> lab-558752{{"How to check if a shell alias is defined in Linux"}} linux/cat -.-> lab-558752{{"How to check if a shell alias is defined in Linux"}} linux/grep -.-> lab-558752{{"How to check if a shell alias is defined in Linux"}} linux/sed -.-> lab-558752{{"How to check if a shell alias is defined in Linux"}} end

List aliases with alias command

In this step, you will learn about aliases in Linux and how to list them using the alias command.

An alias is a shortcut that allows you to replace a long command with a shorter, more memorable one. This can save you a lot of typing and make your command-line experience more efficient.

For example, instead of typing ls -l every time you want a detailed list of files, you could create an alias like ll that does the same thing.

To see the aliases that are currently set in your terminal session, simply type the alias command and press Enter:

alias

You will see a list of aliases, similar to this:

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'

This output shows the alias name on the left (e.g., ll) and the command it represents on the right (e.g., ls -alF).

Notice the ls alias. It's set to ls --color=auto. This means that whenever you type ls, the system actually executes ls --color=auto, which adds color to the output for better readability.

Understanding aliases is the first step to customizing your terminal environment and making it work better for you.

Click Continue to proceed to the next step.

Check user aliases in ~/.bashrc

In the previous step, you saw a list of aliases using the alias command. Now, let's explore where some of these aliases are defined.

User-specific aliases are often stored in configuration files within your home directory. For users of the Bash shell, a common place for this is the ~/.bashrc file. Although this lab environment uses zsh, understanding the role of ~/.bashrc is important as it's widely used in many Linux systems.

The ~ symbol is a shortcut for your home directory, which is /home/labex in this environment. So, ~/.bashrc refers to the .bashrc file located directly in your home directory. The dot (.) at the beginning of the filename makes it a hidden file, meaning it won't show up with a simple ls command unless you use the -a option.

Let's view the contents of the ~/.bashrc file using the cat command. cat is used to display the content of files.

Type the following command and press Enter:

cat ~/.bashrc

You will see the content of the .bashrc file. Scroll through the output to look for lines that start with alias.

## some example aliases
## alias ll='ls -l'
## alias la='ls -a'

## Add an "alert" alias for long running commands.  Use like:
##   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$//'\'')"'

## 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

## enable programmable completion features (you don't need to enable
## this, if it's already enabled in /etc/bash.bashrc and /etc/profile
## sources /etc/bash.bashrc).
if ! shopt -oq posix; then
  if [ -f /usr/share/bash-completion/bash_completion ]; then
    . /usr/share/bash-completion/bash_completion
  elif [ -f /etc/bash_completion ]; then
    . /etc/bash_completion
  fi
fi

You might notice that some lines starting with alias are commented out with a #. These are examples or inactive aliases. The active aliases are the ones without the # at the beginning of the line.

This file is where you would typically add your own custom aliases if you were using Bash. For example, you could add a line like alias update='sudo apt update && sudo apt upgrade' to create an alias for updating your system.

Click Continue to move on and explore system-wide aliases.

Inspect system aliases in /etc/bash.bashrc

In addition to user-specific configuration files like ~/.bashrc, there are also system-wide configuration files that apply to all users on the system. For Bash, one such file is /etc/bash.bashrc.

This file often contains default settings and aliases that the system administrator wants to be available to everyone.

Since this file is located in the /etc directory, which is typically reserved for system configuration, you might need administrator privileges to modify it. However, we can view its contents using cat without special permissions.

Let's view the contents of the /etc/bash.bashrc file. Type the following command and press Enter:

cat /etc/bash.bashrc

You will see the content of the system-wide Bash configuration file. Look for lines that define aliases.

## System-wide .bashrc file for interactive bash(1) shells.

## To enable the setting of the locale environment variables see
## /etc/profile.d/locale.sh. By default in Ubuntu OnLine,
## this is done from /etc/profile.

## If not running interactively, don't do anything
[ -z "$PS1" ] && return

## check the window size after each command and, if necessary,
## update the values of LINES and COLUMNS.
shopt -s checkwinsize

## If set, the pattern "**" used in a pathname expansion context should
## match only directories and subdirectories in addition to the contents of
## the current directory.
#shopt -s globstar

## make less more friendly for non-text input files, see lesspipe(1)
[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"

## set variable identifying the chroot you work in (used in the prompt)
if [ -z "$debian_chroot" ] && [ -r /etc/debian_chroot ]; then
    debian_chroot=$(cat /etc/debian_chroot)
fi

## 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;34):quote=01;32'

## some more ls aliases
#alias ll='ls -l' #alias ll='ls -alF'
#alias la='ls -a' #alias la='ls -AF'
#alias l='ls -CF'

## Add an alias for the 'alert' command
#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$//'\'')"'

## enable programmable completion features (you don't need to enable
## this, if it's already enabled in /etc/bash.bashrc and /etc/profile
## sources /etc/bash.bashrc).
if ! shopt -oq posix; then
  if [ -f /usr/share/bash-completion/bash_completion ]; then
    . /usr/share/bash-completion/bash_completion
  elif [ -f /etc/bash_completion ]; then
    . /etc/bash_completion
  fi
fi

You should see some of the same aliases you saw with the alias command, such as the ls alias with color support. This confirms that some of the default aliases you use are set system-wide.

Understanding where aliases are defined helps you troubleshoot unexpected behavior and customize your environment effectively.

You have now learned how to list active aliases and inspect common configuration files where they are stored.

Click Continue to complete this lab.

Summary

In this lab, you learned how to check if a shell alias is defined in Linux. You started by using the alias command to list the aliases currently set in your terminal session, understanding that aliases are shortcuts for longer commands.

You then explored common locations where aliases are defined, specifically focusing on user-specific aliases often found in the ~/.bashrc file, and system-wide aliases which can be located in files like /etc/bash.bashrc. This process helps you understand how aliases are configured and where to look for their definitions.