语法格式
alias cmdalias=’command [option] [argument]’
alias是个内部命令[root@kedacom test]# type aliasalias 是 shell 内嵌
查看定义的别名
[root@kedacom test]# aliasalias cp='cp -i'alias egrep='egrep --color=auto'alias fgrep='fgrep --color=auto'alias grep='grep --color=auto'alias l.='ls -d .* --color=auto'alias ll='ls -l --color=auto'alias ls='ls --color=auto'alias mv='mv -i'alias rm='rm -i'alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
定义别名
查看eth0网卡的信息,使用ifconfig eth0
[root@kedacom test]# ifconfig eth0eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500inet 172.16.248.63 netmask 255.255.248.0 broadcast 172.16.255.255ether 00:14:10:29:9a:4b txqueuelen 1000 (Ethernet)RX packets 15329477 bytes 1974923289 (1.8 GiB)RX errors 0 dropped 205695 overruns 0 frame 0TX packets 12939800 bytes 1904590364 (1.7 GiB)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0device memory 0x9d400000-9d41ffff
定义别名代替ifconfig eth0
#定义别名if1[root@kedacom test]# alias if1='ifconfig eth0'#使用if1命令[root@kedacom test]# if1eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500inet 172.16.248.63 netmask 255.255.248.0 broadcast 172.16.255.255ether 00:14:10:29:9a:4b txqueuelen 1000 (Ethernet)RX packets 15342286 bytes 1976226430 (1.8 GiB)RX errors 0 dropped 205913 overruns 0 frame 0TX packets 12949890 bytes 1905725209 (1.7 GiB)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0device memory 0x9d400000-9d41ffff#查看当前别名[root@kedacom test]# aliasalias cp='cp -i'alias egrep='egrep --color=auto'alias fgrep='fgrep --color=auto'alias grep='grep --color=auto'alias if1='ifconfig eth0'alias l.='ls -d .* --color=auto'alias ll='ls -l --color=auto'alias ls='ls --color=auto'alias mv='mv -i'alias rm='rm -i'alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
定义别名与原命令同名
[root@kedacom test]# alias ifconfig='ifconfig eth0'[root@kedacom test]# ifconfigeth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500inet 172.16.248.63 netmask 255.255.248.0 broadcast 172.16.255.255ether 00:14:10:29:9a:4b txqueuelen 1000 (Ethernet)RX packets 15347703 bytes 1976770705 (1.8 GiB)RX errors 0 dropped 206003 overruns 0 frame 0TX packets 12954140 bytes 1906202629 (1.7 GiB)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0device memory 0x9d400000-9d41ffff
此时,若需使原命令生效,使用\ifconfig
[root@kedacom test]# \ifconfigeth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500inet 172.16.248.63 netmask 255.255.248.0 broadcast 172.16.255.255ether 00:14:10:29:9a:4b txqueuelen 1000 (Ethernet)RX packets 15349358 bytes 1976933725 (1.8 GiB)RX errors 0 dropped 206031 overruns 0 frame 0TX packets 12955456 bytes 1906350723 (1.7 GiB)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0device memory 0x9d400000-9d41fffflo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536inet 127.0.0.1 netmask 255.0.0.0loop txqueuelen 1000 (Local Loopback)RX packets 7905880 bytes 773861846 (738.0 MiB)RX errors 0 dropped 0 overruns 0 frame 0TX packets 7905880 bytes 773861846 (738.0 MiB)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
别名生效时期
临时生效
通过alias命令生成的别名,只在当前shell中生效,新shell或关闭当前shell后重新连接则无法使用定义的别名
永久生效
通过更改当前用户主目录的隐藏文件.bashrc 可使别名永久生效
[root@kedacom test]# pwd/opt/test#切换至当前用户的主目录[root@kedacom test]# cd[root@kedacom ~]# pwd/root[root@kedacom ~]# lsconf logs#前面带.的是隐藏文件,通过ls -a可查看[root@kedacom ~]# ls -a. .bash_history .bash_profile .cache .config .java .mysql_history .rediscli_history .tcshrc.. .bash_logout .bashrc conf .cshrc logs .oracle_jre_usage .rnd
查看.bashrc 文件内容,包含别名的配置
[root@kedacom ~]# cat .bashrc# .bashrc# User specific aliases and functionsalias rm='rm -i'alias cp='cp -i'alias mv='mv -i'# Source global definitionsif [ -f /etc/bashrc ]; then. /etc/bashrcfi
将命令添加到用户环境配置文件.bashrc,使用vi .bashrc编辑,输入i进入编辑模式,添加alias ifconfig=’ifconfig eth0’后,输入esc键,输入:wq
若需要使别名所有用户都生效,则编辑/etc/bashrc配置文件。
删除别名
unalias 别名名称
[root@kedacom kedacom]# aliasalias cp='cp -i'alias egrep='egrep --color=auto'alias fgrep='fgrep --color=auto'alias grep='grep --color=auto'alias if1='ifconfig eth0'alias l.='ls -d .* --color=auto'alias ll='ls -l --color=auto'alias ls='ls --color=auto'alias mv='mv -i'alias rm='rm -i'alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'#删除别名if1[root@kedacom kedacom]# unalias if1[root@kedacom kedacom]# aliasalias cp='cp -i'alias egrep='egrep --color=auto'alias fgrep='fgrep --color=auto'alias grep='grep --color=auto'alias l.='ls -d .* --color=auto'alias ll='ls -l --color=auto'alias ls='ls --color=auto'alias mv='mv -i'alias rm='rm -i'alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
