linux – 如果我使用位于PATH中的二进制文件的名称命名bash脚本函数会发生什么?

linux – 如果我使用位于PATH中的二进制文件的名称命名bash脚本函数会发生什么?,第1张

概述假设我在bash脚本中编写了一个函数,其中包含可用二进制文件的名称,比如pwd: function pwd(){ echo '/'} 好吧,这看起来有点奇怪,但问题是:如果在我的脚本中进一步编写命令会发生什么: cd /usrpwd 将使用什么密码?另外,我如何强制使用另一个呢? 您的函数将被调用,因为它隐藏了内置的pwd. 要强制执行命令,请使用内置命令: command pwd 从 假设我在bash脚本中编写了一个函数,其中包含可用二进制文件的名称,比如pwd:
function pwd(){    echo '/'}

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

cd /usrpwd

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

解决方法 您的函数将被调用,因为它隐藏了内置的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.
总结

以上是内存溢出为你收集整理的linux – 如果我使用位于PATH中的二进制文件的名称命名bash脚本函数会发生什么?全部内容,希望文章能够帮你解决linux – 如果我使用位于PATH中的二进制文件的名称命名bash脚本函数会发生什么?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/1033365.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-24
下一篇2022-05-24

发表评论

登录后才能评论

评论列表(0条)

    保存