加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

linux – 如果我使用位于PATH中的二进制文件的名称命名bash脚本

发布时间:2020-12-14 02:22:49 所属栏目:Linux 来源:网络整理
导读:假设我在bash脚本中编写了一个函数,其中包含可用二进制文件的名称,比如pwd: function pwd(){ echo '/'} 好吧,这看起来有点奇怪,但问题是:如果在我的脚本中进一步编写命令会发生什么: cd /usrpwd 将使用什么密码?另外,我如何强制使用另一个呢? 解决方法
假设我在bash脚本中编写了一个函数,其中包含可用二进制文件的名称,比如pwd:
function pwd(){
    echo '/'
}

好吧,这看起来有点奇怪,但问题是:如果在我的脚本中进一步编写命令会发生什么:

cd /usr
pwd

将使用什么密码?另外,我如何强制使用另一个呢?

解决方法

您的函数将被调用,因为它隐藏了内置的pwd.

要强制执行命令,请使用内置命令:

command pwd

从bash手册:

command [-pVv] command [arg ...]
          Run command with args  suppressing  the  normal  shell  function
          lookup.  Only builtin commands or commands found in the PATH are
          executed.  If the -p option is given,the search for command  is
          performed  using  a default value for PATH that is guaranteed to
          find all of the standard utilities.  If  either  the  -V  or  -v
          option is supplied,a description of command is printed.  The -v
          option causes a single word indicating the command or file  name
          used to invoke command to be displayed; the -V option produces a
          more verbose description.  If the -V or -v option  is  supplied,the  exit  status  is  0 if command was found,and 1 if not.  If
          neither option is supplied and an error occurred or command can-
          not  be found,the exit status is 127.  Otherwise,the exit sta-
          tus of the command builtin is the exit status of command.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读