alias
命令: alias
功能说明:设置命令的别名
语法:alias [命令]=‘其他命令‘
例:
[[email protected] data]# alias
alias cp=‘cp -i‘
alias l.=‘ls -d .* --color=auto‘
alias ll=‘ls -l --color=auto‘
alias ls=‘ls --color=auto‘
alias mv=‘mv -i‘
alias net=‘cat /etc/sysconfig/network-scripts/ifcfg-eth0‘
alias rm=‘rm -i‘
alias which=‘alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde‘
说明:
①如果登录的用户1 想临时设置别名,用户1 只需执行alias [命令]=‘其他命令‘,该命令的别名只对用户1有效,对其他用户无效,并且用户1 只在该次登录有效,下次登录后失效;
例:
◆ yonghu1设置rm的别名,别名对yonghu1临时有效;
[[email protected] ~]$ alias rm=‘echo Do not use the rm command.‘
[[email protected] ~]$ rm /data/1.txt.ln
Do not use the rm command. /data/1.txt.ln
[[email protected] ~]$
◆ yonghu2进行rm命令的操作,rm别名无效;
[[email protected] ~]$ whoami
yonghu2
[[email protected] ~]$ rm /data/1.txt.ln 【yonghu2 执行rm,yonghu1设置的rm别名无效】
rm: remove write-protected regular file `/data/1.txt.ln‘?
◆ yonghu1注销后重新登录,上次设置的rm别名已经无效
[[email protected] ~]$ logout
[[email protected] ~]# su - yonghu1
[[email protected] ~]$ whoami
yonghu1
[[email protected] ~]$ rm /data/1.txt.ln 【yonghu1 再次执行rm,上次登录设置的rm别名失效】
rm: remove write-protected regular file `/data/1.txt.ln‘?
②如果用户1想让本次登录设置的别名在下次登录时还有效,就需要更该~/.bashrc 或者~/.bash_profile。方法是将alias [命令]=‘其他命令‘ 追加到~/.bashrc 或者~/.bash_profile末行,并执行source ~/.bashrc 或者source ~/.bash_profile使文件生效即可。(更改~/.bashrc或者~/.bash_profile只对当前单用户有效,对其他用户无效)
这里以修改~/.bashrc为例:
●yonghu1在~/.bashrc设置rm的别名,别名对yonghu1有效;
[[email protected] ~]$ echo "alias rm=‘echo Do not use the rm command.‘">>~/.bash
[[email protected] ~]$ tail -1 ~/.bashrc
alias rm=‘echo Do not use the rm command.‘
[[email protected] ~]$ source ~/.bashrc
[[email protected] ~]$ rm
Do not use the rm command.
●yonghu1重新登录,rm别名对yonghu1仍然有效,长期有效;
[[email protected] ~]$ logout
[[email protected] ~]$ rm
Do not use the rm command.
●yonghu2在执行rm的命令,别名对yonghu2有效;
[[email protected] ~]$ rm /data/1.txt.ln
rm: remove write-protected regular file `/data/1.txt.ln‘?
[[email protected] ~]$ rm
Do not use the rm command.
③如果设置的别名对所有用户都有效,就要修改环境变量文件/etc/profile 或者/etc/bashrc,方法是将alias [命令]=‘其他命令‘ 追加到/etc/profile 或者/etc/bashrc 末行,并对应执行source /etc/profile 或者/etc/bashrc 使文件生效即可,该操作是永久生效的,但是一般用户没有权限修改,需要root权限。
这里以修改/etc/profile为例:
★yonghu2在执行修改/etc/profile的命令,将rm别名追加到/etc/profile,没有权限;
[[email protected] root]$ whoami
yonghu2
[[email protected] root]$ echo "alias rm=‘echo Do not use the rm command.‘">>/etc/profile
bash: /etc/profile: Permission denied
......
★超级用户root在执行修改/etc/profile的命令,将rm别名追加到/etc/profile,rm别名生效,永久生效;
[[email protected] ~]# whoami
root
[[email protected] ~]# echo "alias rm=‘echo Do not use the rm command.‘">>/etc/profile
[[email protected] ~]# tail -1 /etc/profile
alias rm=‘echo Do not use the rm command.‘
[[email protected] ~]# source /etc/profile
[[email protected] ~]# rm
Do not use the rm command.
......
★用户2在执行rm命令,rm别名也是生效的;
[[email protected] ~]$ whoami
yonghu2
[[email protected] ~]$ rm
Do not use the rm command.
[[email protected] ~]$
★用户重新登录,然后再次执行rm命令,别名也是生效的;
[[email protected] ~]$ rm
Do not use the rm command.
[[email protected] ~]$
说明:
1、/etc/bashrc 和 ~/.bashrc 区别就在于/etc/bashrc 设置相当于国法,设置给全系统;~/.bashrc是家规,设置给单用户使用;
2、/etc/profile 和 ~/.bash_profile区别就在于/etc/profile 设置相当于国法,设置给全系统;~/.bash_profile 是家规,设置给单用户使用;
3、单用户设置包括超级用户root,root设置~/.bashrc 或者~/.bash_profile只对root有效;
4、至于/etc/profile 与/etc/bashrc的区别在以后再细谈,这里暂时可以认为都是国法,效果一样;
5、至于~/.bash_profile 与~/.bashrc的区别在以后再细谈,这里暂时可以认为都是家规,效果一样;