How to create a permanent Bash alias on Linux/Unix

I need to create a bash shell alias named update as follows:
alias update='sudo -- sh -c "/root/bin/chk_disk && dnf update'
However, this update alias gets removed after I reboot the Fedora Linux box. How do I create a permanent Bash alias on a Fedora Linux or Unix-like system?

You need to put bash shell aliases in the ~/.bashrc file ($HOME/.bashrc) file executed by bash for non-login shells. On most modern Linux distros, you may want to put all your bash alias definitions into a separate file like ~/.bash_aliases, instead of adding them in the ~/.bashrc file directly. This tutorial shows how to create and add aliases permanently to your bash shell on Linux and Unix-like systems.
Tutorial details
Difficulty level Easy
Root privileges No
Requirements Bash running on Linux, macOS or Unix
Est. reading time 2 minutes

Steps to create a permanent Bash alias:

Open the Terminal app and then type the following commands:

  1. Edit ~/.bash_aliases or ~/.bashrc file using: vi ~/.bash_aliases
  2. Append your bash alias
  3. For example append: alias update='sudo yum update'
  4. Save and close the file.
  5. Activate alias by typing: source ~/.bash_aliases

Please note that ~/.bash_aliases file only works if the following line presents in the ~/.bashrc file:

if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi

Are above lines are missing in your ~/.bashrc file? Just append at the end of the ~/.bashrc, using a text editor such as vi/vim or joe.

Examples

Let us create four aliases as follows:
$ vi ~/.bash_aliases
OR
$ joe ~/.bashrc
Append text:

# update our debian/ubuntu box
alias update='sudo -- sh -c "apt update && apt upgrade"'
 
# make grep output colorful 
alias grep='grep --color=auto'
 
# set eth0 as an interface for eth0  
alias vnstat='vnstat -i eth0'
 
# flush redis cache for wp
alias flush_redis='redis-cli -h 127.0.0.1 FLUSHDB'

Save and close the file.

How to load aliases

All new aliases will be available next time you login using a new ssh/terminal session. To load changes immediately, type the following source command:
$ source ~/.bash_aliases
OR
$ . ~/.bash_aliases

How to list all of my aliases

To list all aliases, run:
$ alias
Sample outputs:

alias flush_redis='redis-cli -h 127.0.0.1 FLUSHDB'
alias grep='grep --color=auto'
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alF'
alias ls='ls --color=auto'
alias update='sudo -- sh -c "apt update && apt upgrade"'
alias vnstat='vnstat -i eth0'

How to use/call aliases

Just type alias name:
$ update
$ vnstat
$ flush_redis

Removing bash aliases

To remove given alias from the list of defined aliases, try the unalias command:
unalias alias_name
unalias c

Delete alias definitions by passing the -a as follows:
unalias -a

Summary of all commands

You learned how to create bash aliases using the alias command.

Command Description Example(s)
alias
alias -p
Prints the list of aliase alias
alias name='value' Define aliase alias c='clear'
alias d='df -H'
c
d
Execute alias called c or d c
d
unalias NAME
unalias -a
Delete each NAME from the list of defined aliases
The -a option remove all alias definitions
unalias c
unalias -a
vi ~/.bashrc
nano ~/.bashrc
vim ~/.bashr_aliases
neovim ~/.bashr_aliases
Store your aliases permanently in bash config file
alias cp='cp -i'
alias mv='mv -i'
alias rm='rm -i'
alias ls='ls --color=auto'
alias l.='ls -d .*'
alias vnstat='vnstat -i eth0'

See also 30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X and bash command man page here.


🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 5 comments so far... add one


CategoryList of Unix and Linux commands
Disk space analyzersdf duf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Modern utilitiesbat exa
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg glances gtop jobs killall kill pidof pstree pwdx time vtop
Searchingag grep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
5 comments… add one
  • Anonymous Feb 27, 2021 @ 14:10

    How to create bash aliases ? Such is topic for advanced Linux user. But, for me it is so confusing and this page was refreshing tutorial. Pretty pretty good page.

  • Anon Mar 30, 2021 @ 2:09

    This only works if I do the “source ~./bash_aliases” command every time I open a window. How do I fix it so i don’t have to do that??

    • 🐧 Vivek Gite Mar 30, 2021 @ 9:42

      Read the page again. It says,

      Please note that ~/.bash_aliases file only works if the following line presents in the ~/.bashrc file:

      if [ -f ~/.bash_aliases ]; then
      . ~/.bash_aliases
      fi

      • Anonymous Jun 1, 2021 @ 6:34

        I have the same issue as Anon.

        if [ -f ~/.bash_aliases ]; then
        . ~/.bash_aliases
        fi

        is at the end of my bashrc yet I have to use the source command each time

      • J Jun 1, 2021 @ 6:37

        Ive got

        if [ -f ~/.bash_aliases ]; then
        . ~/.bash_aliases
        fi 

        included in bashrc and I also have to use the source command each time

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum