美文网首页
Linux终端命令PATH的搜索

Linux终端命令PATH的搜索

作者: 非典型IT | 来源:发表于2018-09-29 17:47 被阅读0次

结论

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

相关文章

  • Linux终端命令PATH的搜索

    结论: 为什么? 默认打开的-h选项会记住命令旧的路径,在同一session中,再次执行相同命令时,不再进行命令的...

  • Linux 2-常用 Linux 命令的基本使用

    目标 理解学习 Linux 终端命令的原因 常用 Linux 命令体验 01. 学习 Linux 终端命令的原因 ...

  • Linux终端

    Linux终端 介绍Linux终端、命令行以及执行系统命令。对于新手,需要熟悉Linux终端,因为终端是与Linu...

  • Mac下启动Mysql

    1.进入终端 2.在终端中输入添加MySQL路径的命令: PATH="$PATH":/usr/local/mysq...

  • 2019-07-11

    Linux 终端命令格式 目标 了解终端命令格式 知道如何查阅终端命令帮助信息 终端命令格式command [-o...

  • MAC OS - MYSQL初始密码修改

    启动MYSQL服务 在终端里执行命令PATH=”$PATH":/usr/local/mysql/bin

  • 快速操作Linux终端命令行的快捷键列表

    转自:快速操作Linux终端命令行的快捷键列表 快速操作Linux终端命令行的快捷键列表 在shell命令终端中,...

  • Linux路径加入环境变量

    PATH=$PATH:/:/后面加路径。PATH是环境变量,要大写那几个目录是你放置linux命令的目录,输入命令...

  • linux每日笔记---day 1

    1、Linux终端简单介绍 linux命令行也成为Linux终端界面(terminal或console),strl...

  • Linux的基本命令一

    一、Linux终端 windows下的cmd在Linux中叫做终端(Terminal),命令都是在终端内执行的 二...

网友评论

      本文标题:Linux终端命令PATH的搜索

      本文链接:https://www.haomeiwen.com/subject/gpeboftx.html