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 distro, 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 add aliases permanently to your bash shell.

Steps to create a permanent Bash alias:

Open the Terminal app

  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

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.


Let us create four aliases as follows:
$ vi ~/.bash_aliases
$ 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 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
$ . ~/.bash_aliases

How to list all of my aliases

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

alias flush_redis='redis-cli -h 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

Summary of all commands

Command Description Example(s)
alias -p
Prints the list of aliase alias
alias name='value' Define aliase alias c='clear'
alias d='df -H'
Execute alias called c or d NA
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
joe ~/.bashrc
vi ~/.bashr_aliases
joe ~/.bashr_aliases
Store your aliases permanently in bash config file NA

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

Posted by: SXI ADMIN

The author is the creator of SXI LLC and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.