BASH环境

一 什么是SHELL

shell一般代表两个层面的意思,一个是命令解释器,比如BASH,另外一个就是shell脚本。本节我们站在命令解释器的角度来阐述shell。

二 命令的优先级

命令分为:

==> alias
  ==> Compound Commands
    ==> function 
      ==> build_in
        ==> hash
          ==> $PATH
            ==> error: command not found

=================================part1

alias:别名

查看已定义的别名:

[root@python-main ~]# alias
alias 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'

定义新的别名:

[root@python-main ~]# alias test='ll -h --color=auto'
[root@python-main ~]# test
总用量 144K
-rw-------. 1 root root 1.6K 10月  6 19:22 anaconda-ks.cfg
-rw-r--r--. 1 root root 1.6K 10月  6 19:28 initial-setup-ks.cfg

取消别名:

[root@python-main /]# unalias test
[root@python-main /]# test
[root@python-main /]# echo $?
1

ps:登陆后的预置别名从何而来(别名应该学会的东西:取消别名和建立别名,固化别名配置我们会在后续章节介绍,无非将alias定义到文件中):

一部分来自/root/.bashrc 普通用户的.bashrc不包含别名,因为普通用户的.bashrc模板是:/etc/skel/.bashrc
而root这个.bashrc在安装完系统就预置了.
[root@MiWiFi-R3-srv ~]# grep '^alias' /root/.bashrc 
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
[root@seker ~]#
一部分来自/etc/profile.d/目录里的可执行文件

[root@MiWiFi-R3-srv ~]# grep -rn 'alias' /etc/profile.d/
/etc/profile.d/colorgrep.csh:9:alias grep 'grep --color=auto'
/etc/profile.d/colorgrep.csh:10:alias egrep 'egrep --color=auto'
/etc/profile.d/colorgrep.csh:11:alias fgrep 'fgrep --color=auto'
/etc/profile.d/colorgrep.sh:5:alias grep='grep --color=auto' 2>/dev/null
/etc/profile.d/colorgrep.sh:6:alias egrep='egrep --color=auto' 2>/dev/null
/etc/profile.d/colorgrep.sh:7:alias fgrep='fgrep --color=auto' 2>/dev/null
/etc/profile.d/which2.csh:5:# alias which 'alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
/etc/profile.d/which2.sh:4:alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'

Compound Commands:if、for、while
function:

[root@python-main /]# vim test.sh
[root@python-main /]# cat test.sh
function test(){
    read -p 'please input your hostname: ' name;
    hostnamectl set-hostname $name;
    hostname;
}

[root@python-main /]# test
please input your hostname: myworld
myworld

查看已定义的函数:

[root@python-main /]# set

取消已定义的函数:

[root@python-main /]# unset test

build in:BASH自带的命令,即使没有可执行文件也可以执行。

查看build in命令:

[root@python-main /]# compgen -b
.
:
[
alias
bg
bind
break
builtin
caller
cd
command
compgen
complete
compopt
continue
declare
dirs
disown
echo
enable
eval
exec
exit
export
false
fc
fg
getopts
hash
help
history
jobs
kill
let
local
logout
mapfile
popd
printf
pushd
pwd
read
readarray
readonly
return
set
shift
shopt
source
suspend
test
times
trap
true
type
typeset
ulimit
umask
unalias
unset
wait

 hash:

因为这个变量中包含的路径太多,每个路径中的可执行文件也很多
如果每次都要搜索每个路径下的所有可执行文件,显然是不明智的
为了减少$PATH的搜索,上一次搜索的内容能够被下一次执行重用
bash对从$PATH中搜索得出的外部命令建立一个hash表,用于缓存
这个缓存是会话级别独立拥有的.不可以对其他终端共享,因为每个用户的$PATH可能不同

[root@MiWiFi-R3-srv ~]# hash -r #清除缓存
[root@MiWiFi-R3-srv ~]# hostname #每执行一次就缓存一次
MiWiFi-R3-srv
[root@MiWiFi-R3-srv ~]# hash #查看缓存表,发现多了一条缓存

hits command

1 /bin/hostname

[root@MiWiFi-R3-srv ~]# mv /bin/hostname /sbin/ #将hostname命令移动到另外一个目录
[root@MiWiFi-R3-srv ~]# hostname #再次执行hostname,仍然是从缓存中读hostname所在的位置,证明hash优先级比path高,hash缓存的是path路径,而内置命令如cd等没有路径,因而无需罗嗦测试,内置的优先级肯定是比hash要高的
bash: /bin/hostname: No such file or directory

总结命令执行的获取顺序:
==> alias
  ==> Compound Commands
    ==> function
      ==> build_in
        ==> hash
          ==> $PATH
            ==> error: command not found

原文地址:https://www.cnblogs.com/Icarus1900/p/7678636.html