别名就是一种快捷方式,以省去用户输入一长串命令的麻烦。
别名有多种实现方式,可以使用函数,也可以使用alias命令
注意:alias命令的作用只是短暂的。一旦终端关闭,别名则失效,如果要让别名永久生效,需要添加到~/.bashrc
案例
1.建立 一个别名pl,使用该别名可以ping本地
[root@localhost test]# alias pl='ping localhost'
[root@localhost test]# pl
PING localhost (127.0.0.1) 56(84) bytes of data.
64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.400 ms
64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.375 ms
64 bytes from localhost (127.0.0.1): icmp_seq=3 ttl=64 time=0.465 ms
64 bytes from localhost (127.0.0.1): icmp_seq=4 ttl=64 time=0.362 ms
64 bytes from localhost (127.0.0.1): icmp_seq=5 ttl=64 time=0.360 ms
64 bytes from localhost (127.0.0.1): icmp_seq=6 ttl=64 time=0.359 ms
64 bytes from localhost (127.0.0.1): icmp_seq=7 ttl=64 time=0.367 ms
64 bytes from localhost (127.0.0.1): icmp_seq=8 ttl=64 time=0.453 ms
64 bytes from localhost (127.0.0.1): icmp_seq=9 ttl=64 time=0.395 ms
^C
--- localhost ping statistics ---
9 packets transmitted, 9 received, 0% packet loss, time 8020ms
rtt min/avg/max/mdev = 0.359/0.392/0.465/0.046 ms
[root@localhost test]#
2.将pl命令添加到~/.bashrc
[root@localhost test]# echo 'alias pl="ping localhost"'>>~/.bashrc
[root@localhost test]# tail ~/.bashrc
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
alias pl="ping localhost"
[root@localhost test]#
3.删除别名
已经添加到.bashrc文件中的需要打开文件删除
其他的使用unalias删除即可。