Linux 如果我用路径中的二进制文件名命名bash脚本函数,会发生什么情况?
假设我在bash脚本中编写了一个函数,其名称为可用的二进制文件,例如pwd:Linux 如果我用路径中的二进制文件名命名bash脚本函数,会发生什么情况?,linux,bash,shell,Linux,Bash,Shell,假设我在bash脚本中编写了一个函数,其名称为可用的二进制文件,例如pwd: function pwd(){ echo '/' } 好吧,这似乎有点奇怪,但问题是:如果在我的脚本中进一步编写命令,会发生什么: cd /usr pwd 将使用什么pwd?另外,如何强制使用另一个?您的函数将被调用,因为它隐藏了pwd内置项 要强制执行命令,请使用命令内置: command pwd 从bash手册: command [-pVv] command [arg ...]
function pwd(){
echo '/'
}
好吧,这似乎有点奇怪,但问题是:如果在我的脚本中进一步编写命令,会发生什么:
cd /usr
pwd
将使用什么
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.
函数将具有优先权。您可以使用
类型pwd
轻松检查它
考虑到pwd
是内置的,您可以使用builtinpwd
实现真正的实现
如果您想从系统中实际获取可执行文件,可以参考其路径,例如使用
$(哪个pwd)
函数优先于路径搜索。使用完整路径以避免插入函数。builtin
关键字为Zsh中Bash的内置关键字(echo
,cd
,等等)提供了类似的功能,对别名执行的内置关键字将打印别名值。