Linux终端命令PATH的搜索

结论

Linux会cache命令的路径,即使你在PATH中新增了该命令,也还是会读取旧路径的命令。

比如,如下操作:
1. /bin/下有文件 a.sh
2. 执行 a.sh
3. 我在PATH增加路径/root/huayd到PATH的最前面,并放上文件a.sh。
4. 执行a.sh,此时还是执行的是 /bin下的a.sh,而不是 /root/huayd下的

为什么?

shell的默认选项:
[root@huayd1 ~]# echo $-
himBH

set [--abefhkmnptuvxBCEHPT] [-o option-name] [arg ...]
   set [+abefhkmnptuvxBCEHPT] [+o option-name] [arg ...]
          Without  options, the name and value of each shell variable are displayed in a format that can be reused as
          input for setting or resetting the currently-set variables.  Read-only variables cannot be reset.  In posix
          mode, only shell variables are listed.  The output is sorted according to the current locale.  When options
          are specified, they set or unset shell attributes.  Any arguments remaining  after  option  processing  are
          treated  as  values for the positional parameters and are assigned, in order, to $1, $2, ...  $n.  Options,
          if specified, have the following meanings:
          
          ......
          -h      Remember the location of commands as they are looked up for execution.  This is enabled by default.

默认打开的-h选项会记住命令旧的路径,在同一session中,再次执行相同命令时,不再进行命令的路径寻找。

场景重现如下:

[root@huayd1 huayd]# chmod +x /usr/bin/a.sh
[root@huayd1 huayd]# 
[root@huayd1 huayd]# a.sh
b
[root@huayd1 huayd]# 
[root@huayd1 huayd]# export PATH=/root/huayd:$PATH
[root@huayd1 huayd]# 
[root@huayd1 huayd]# a.sh
b
[root@huayd1 huayd]# 
[root@huayd1 huayd]# echo $PATH
/root/huayd:/root/huayd:/usr/lib64/qt-3.3/bin:/root/perl5/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/root/bin:/root/go/bin:/usr/local/mysql-proxy/bin:/root/bin:/root/go/bin:/usr/local/mysql-proxy/bin
[root@huayd1 huayd]# 
[root@huayd1 huayd]# chmod +x *
[root@huayd1 huayd]# ./a.sh 
a
[root@huayd1 huayd]# 
[root@huayd1 huayd]# a.sh 
b
[root@huayd1 huayd]# 
[root@huayd1 huayd]# find / -name 'a.sh'
/root/huayd/a.sh
/usr/bin/a.sh
[root@huayd1 huayd]# 
[root@huayd1 huayd]# cat /root/huayd/a.sh
echo a
[root@huayd1 huayd]# 
[root@huayd1 huayd]# cat /usr/bin/a.sh
echo b
[root@huayd1 huayd]# 

--End
Mason

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 官网 中文版本 好的网站 Content-type: text/htmlBASH Section: User ...
    不排版阅读 9,960评论 0 5
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,833评论 19 139
  • 系统管理与维护命令 date date(选项)(参数) | 选项 | 说明 | | :-------- | ...
    蓓蓓的万能男友阅读 9,467评论 0 5
  • linux资料总章2.1 1.0写的不好抱歉 但是2.0已经改了很多 但是错误还是无法避免 以后资料会慢慢更新 大...
    数据革命阅读 14,261评论 2 33
  • 在《孩子的兴趣需要慢慢熬》这篇文章中,我提到过一本我个人比较喜欢的书,美国认知心理学家Daniel T. Will...
    婵婵阅读 5,776评论 6 9

友情链接更多精彩内容