How to create a permanent Bash alias on Linux/Unix

alias update = 'sudo -- sh -c "/root/bin/chk_disk && dnf update'

ADVERTISEMENTS



Steps to create a permanent Bash alias:

I need to create a bash shell alias named update as follows: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.

Open the Terminal app

Edit ~/.bash_aliases or ~/.bashrc file using: vi ~/.bash_aliases Append your bash alias For example append: alias update='sudo yum update' Save and close the file. 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 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' # 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' 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

Summary of all commands

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