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 on Linux and Unix-like systems.
Steps to create a permanent Bash alias:
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
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
- Share on Twitter • Facebook • 0 comments... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |