语法格式

alias cmdalias=’command [option] [argument]’

  1. alias是个内部命令
  2. [root@kedacom test]# type alias
  3. alias shell 内嵌

查看定义的别名

  1. [root@kedacom test]# alias
  2. alias cp='cp -i'
  3. alias egrep='egrep --color=auto'
  4. alias fgrep='fgrep --color=auto'
  5. alias grep='grep --color=auto'
  6. alias l.='ls -d .* --color=auto'
  7. alias ll='ls -l --color=auto'
  8. alias ls='ls --color=auto'
  9. alias mv='mv -i'
  10. alias rm='rm -i'
  11. alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'

定义别名

查看eth0网卡的信息,使用ifconfig eth0

  1. [root@kedacom test]# ifconfig eth0
  2. eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
  3. inet 172.16.248.63 netmask 255.255.248.0 broadcast 172.16.255.255
  4. ether 00:14:10:29:9a:4b txqueuelen 1000 (Ethernet)
  5. RX packets 15329477 bytes 1974923289 (1.8 GiB)
  6. RX errors 0 dropped 205695 overruns 0 frame 0
  7. TX packets 12939800 bytes 1904590364 (1.7 GiB)
  8. TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
  9. device memory 0x9d400000-9d41ffff

定义别名代替ifconfig eth0

  1. #定义别名if1
  2. [root@kedacom test]# alias if1='ifconfig eth0'
  3. #使用if1命令
  4. [root@kedacom test]# if1
  5. eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
  6. inet 172.16.248.63 netmask 255.255.248.0 broadcast 172.16.255.255
  7. ether 00:14:10:29:9a:4b txqueuelen 1000 (Ethernet)
  8. RX packets 15342286 bytes 1976226430 (1.8 GiB)
  9. RX errors 0 dropped 205913 overruns 0 frame 0
  10. TX packets 12949890 bytes 1905725209 (1.7 GiB)
  11. TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
  12. device memory 0x9d400000-9d41ffff
  13. #查看当前别名
  14. [root@kedacom test]# alias
  15. alias cp='cp -i'
  16. alias egrep='egrep --color=auto'
  17. alias fgrep='fgrep --color=auto'
  18. alias grep='grep --color=auto'
  19. alias if1='ifconfig eth0'
  20. alias l.='ls -d .* --color=auto'
  21. alias ll='ls -l --color=auto'
  22. alias ls='ls --color=auto'
  23. alias mv='mv -i'
  24. alias rm='rm -i'
  25. alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'

定义别名与原命令同名

  1. [root@kedacom test]# alias ifconfig='ifconfig eth0'
  2. [root@kedacom test]# ifconfig
  3. eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
  4. inet 172.16.248.63 netmask 255.255.248.0 broadcast 172.16.255.255
  5. ether 00:14:10:29:9a:4b txqueuelen 1000 (Ethernet)
  6. RX packets 15347703 bytes 1976770705 (1.8 GiB)
  7. RX errors 0 dropped 206003 overruns 0 frame 0
  8. TX packets 12954140 bytes 1906202629 (1.7 GiB)
  9. TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
  10. device memory 0x9d400000-9d41ffff

此时,若需使原命令生效,使用\ifconfig

  1. [root@kedacom test]# \ifconfig
  2. eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
  3. inet 172.16.248.63 netmask 255.255.248.0 broadcast 172.16.255.255
  4. ether 00:14:10:29:9a:4b txqueuelen 1000 (Ethernet)
  5. RX packets 15349358 bytes 1976933725 (1.8 GiB)
  6. RX errors 0 dropped 206031 overruns 0 frame 0
  7. TX packets 12955456 bytes 1906350723 (1.7 GiB)
  8. TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
  9. device memory 0x9d400000-9d41ffff
  10. lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
  11. inet 127.0.0.1 netmask 255.0.0.0
  12. loop txqueuelen 1000 (Local Loopback)
  13. RX packets 7905880 bytes 773861846 (738.0 MiB)
  14. RX errors 0 dropped 0 overruns 0 frame 0
  15. TX packets 7905880 bytes 773861846 (738.0 MiB)
  16. TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

别名生效时期

临时生效

通过alias命令生成的别名,只在当前shell中生效,新shell或关闭当前shell后重新连接则无法使用定义的别名

永久生效

通过更改当前用户主目录的隐藏文件.bashrc 可使别名永久生效

  1. [root@kedacom test]# pwd
  2. /opt/test
  3. #切换至当前用户的主目录
  4. [root@kedacom test]# cd
  5. [root@kedacom ~]# pwd
  6. /root
  7. [root@kedacom ~]# ls
  8. conf logs
  9. #前面带.的是隐藏文件,通过ls -a可查看
  10. [root@kedacom ~]# ls -a
  11. . .bash_history .bash_profile .cache .config .java .mysql_history .rediscli_history .tcshrc
  12. .. .bash_logout .bashrc conf .cshrc logs .oracle_jre_usage .rnd

查看.bashrc 文件内容,包含别名的配置

  1. [root@kedacom ~]# cat .bashrc
  2. # .bashrc
  3. # User specific aliases and functions
  4. alias rm='rm -i'
  5. alias cp='cp -i'
  6. alias mv='mv -i'
  7. # Source global definitions
  8. if [ -f /etc/bashrc ]; then
  9. . /etc/bashrc
  10. fi

将命令添加到用户环境配置文件.bashrc,使用vi .bashrc编辑,输入i进入编辑模式,添加alias ifconfig=’ifconfig eth0’后,输入esc键,输入:wq

若需要使别名所有用户都生效,则编辑/etc/bashrc配置文件。

删除别名

unalias 别名名称

  1. [root@kedacom kedacom]# alias
  2. alias cp='cp -i'
  3. alias egrep='egrep --color=auto'
  4. alias fgrep='fgrep --color=auto'
  5. alias grep='grep --color=auto'
  6. alias if1='ifconfig eth0'
  7. alias l.='ls -d .* --color=auto'
  8. alias ll='ls -l --color=auto'
  9. alias ls='ls --color=auto'
  10. alias mv='mv -i'
  11. alias rm='rm -i'
  12. alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
  13. #删除别名if1
  14. [root@kedacom kedacom]# unalias if1
  15. [root@kedacom kedacom]# alias
  16. alias cp='cp -i'
  17. alias egrep='egrep --color=auto'
  18. alias fgrep='fgrep --color=auto'
  19. alias grep='grep --color=auto'
  20. alias l.='ls -d .* --color=auto'
  21. alias ll='ls -l --color=auto'
  22. alias ls='ls --color=auto'
  23. alias mv='mv -i'
  24. alias rm='rm -i'
  25. alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'