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

前端之家收集整理的这篇文章主要介绍了linux – 如果我使用位于PATH中的二进制文件的名称命名bash脚本函数会发生什么?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
假设我在bash脚本中编写了一个函数,其中包含可用二进制文件名称,比如pwd:
  1. function pwd(){
  2. echo '/'
  3. }

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

  1. cd /usr
  2. pwd

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

解决方法

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

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

  1. command pwd

从bash手册:

  1. command [-pVv] command [arg ...]
  2. Run command with args suppressing the normal shell function
  3. lookup. Only builtin commands or commands found in the PATH are
  4. executed. If the -p option is given,the search for command is
  5. performed using a default value for PATH that is guaranteed to
  6. find all of the standard utilities. If either the -V or -v
  7. option is supplied,a description of command is printed. The -v
  8. option causes a single word indicating the command or file name
  9. used to invoke command to be displayed; the -V option produces a
  10. 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
  11. neither option is supplied and an error occurred or command can-
  12. not be found,the exit status is 127. Otherwise,the exit sta-
  13. tus of the command builtin is the exit status of command.

猜你在找的Linux相关文章