Alias command

From Linux Shell Scripting Tutorial - A Beginner's handbook
Jump to navigation Jump to search

To define or display bash shell aliases use the alias command.

How to view aliases

Type the following command:

alias

Sample outputs:

alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
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"'

How to define alias

The syntax is

alias name=value

To define an alias called vnstat:

alias vnstat='vnstat -i eth0'

How to use alias

Simply type your alias name:

vnstat

Sample output:

Database updated: Thu Nov 16 06:19:15 2017

   eth0 since 07/16/17

          rx:  125.80 TiB      tx:  24.72 TiB      total:  150.51 TiB

   monthly
                     rx      |     tx      |    total    |   avg. rate
     ------------------------+-------------+-------------+---------------
       Oct '17    464.51 GiB |   85.78 GiB |  550.30 GiB |    1.72 Mbit/s
       Nov '17    213.76 GiB |   41.87 GiB |  255.63 GiB |    1.63 Mbit/s
     ------------------------+-------------+-------------+---------------
     estimated    420.14 GiB |   82.30 GiB |  502.44 GiB |

   daily
                     rx      |     tx      |    total    |   avg. rate
     ------------------------+-------------+-------------+---------------
     yesterday     15.87 GiB |    3.00 GiB |   18.87 GiB |    1.83 Mbit/s
         today      2.96 GiB |  528.68 MiB |    3.47 GiB |    1.28 Mbit/s
     ------------------------+-------------+-------------+---------------
     estimated     11.23 GiB |    1.96 GiB |   13.19 GiB |

How to remove each alias from the list of defined aliases

The syntax is:

unalias name

To remove an alias named vnstat:

unalias vnstat

To remove all alias definitions pass the -a option to alias command:

alias -a

How to remove each alias from the list of defined aliases

The syntax is:

unalias name

To remove an alias named vnstat:

unalias vnstat

To remove all alias definitions pass the -a option to alias command:

alias -a

See also