Using Aliases in CentOS
Aliases are a way of assinging a complex command to an easy to remember word. Looking at the command below:
ip addr |grep '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}'
This returns just the IP part of the output from an ip addr command, making the output much easier to read, however remembering and typing the command requires more effort than it saves. For this then we can create an alias.
I want an alias called showip that acutally does the same as typing ip addr |grep '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}'
You need to edit the .bash file for the logged in user who will use this alias. I am logged in as pi, so I will edit that bash file.\
Navigate to root:
cd /root
Show the list of files:
ls -al to show file list, you will see a list of files, including one called .bashrc\\
Edit the .bashrc file by entering:
vi /root/.bashrc
My default .bashrc file looks like this:
# .bashrc # User specific aliases and functions alias rm='rm -i' alias cp='cp -i' alias mv='mv -i' # Source global definitions if [ -f /etc/bashrc ]; then . /etc/bashrc fi
Now add the new alias:
alias showip=" ip addr |grep '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}'"
save the file and exit:
:wq
You have to restart the bash for any new aliases to work
. .bashrc
If you want to see if the alias is now listed, from the command prompt just type:
alias
You should now be able to type your new command at the command line and get an output:
showip inet 127.0.0.1/8 scope host lo inet 192.168.27.30/24 brd 192.168.27.255 scope global eth0