简体   繁体   English

如何知道命令在linux中运行的位置

[英]how to know from where the command ran in linux

Suppose i ran a command in unix like ls or rm and path variable is set as below 假设我在unix中运行命令,如lsrm并且路径变量设置如下

PATH=$JAVA_HOME/bin:.:$ORACLE_HOME/bin:/sbin:/usr/sbin:/usr/bin:/usr/ccs/bin:$COBDIR/bin:/sbin:/bin:/usr/local/bin:/bin

How can we know ls command ran from which path ? 我们如何知道ls命令从哪条路径运行? ls (just an example) may be there in /sbin as well /usr/bin ls (只是一个例子)也可能在/sbin /usr/bin

So i would like to know from where it ran ? 所以我想知道它在哪里运行?

i cannot afford to search all directory and know in which all directory ls is lying. 我无法负担搜索所有目录并知道所有目录ls所在的位置。 is there a direct way to search ls from where it ran? 有没有直接的方法从它运行的地方搜索ls

When you run an external command in bash, bash hashes that command to avoid having to do the path lookup twice. 当您在bash中运行外部命令时,bash会对该命令进行哈希处理,以避免必须两次执行路径查找。 The hash command can tell you which command was run. hash命令可以告诉您运行了哪个命令。 If the command has not been run in the hash's lifetime, it will give an error. 如果该命令尚未在哈希的生命周期中运行,则会出错。

$ hash -t ls
-bash: hash: ls: not found

$ ls foo
$ hash -t ls
/bin/ls

It's advantageous to know how hash , which and the type command differ. 知道hashwhichtype命令的不同之处是有利的。

  • hash tells you what path/command was used/hashed. hash告诉你的路径/命令使用 /散列什么。 If your PATH or filesystem changes during hash 's lifetime, hash can tell you about commands that happened before that change. 如果您的PATH或文件系统在hash生命周期中发生更改,则hash可以告诉您在更改之前发生的命令。
  • which is an external command that looks up a command in the PATH environment variable. which是一个在PATH环境变量中查找命令的外部命令。
  • type is a builtin command that looks up a command in the local PATH variable, which can be (but hardly ever is) different from what's in the environment. type是一个内置命令,用于在本地PATH变量中查找命令,该命令可以(但几乎没有)与环境中的命令不同。

See help hash in your bash to read more about how it works. 请参阅bash中的help hash以了解有关其工作原理的更多信息。

Just use which to locate from where exactly it's going to be picked up from 只需使用哪个来定位它将从哪里获取

$ which ls
  /bin/ls

It will list which path it picked up that executable from. 它将列出从中获取可执行文件的路径。 So from above command, my ls command is in /bin directory. 所以从上面的命令,我的ls命令在/ bin目录中。

In addition to other answers mentioning which , type , hash you could also use the whereis(1) command (if it is installed on your system). 除了提到whichtypehash其他答案,您还可以使用whereis(1)命令(如果它安装在您的系统上)。

whereis tells you the standard places where a command should be. whereis告诉你命令应该在哪里的标准位置。

If your interactive shell is zsh , you could also use the =ls word eg echo =ls or ls =ls to find out which ls is refered by the shell. 如果你的交互式shell是zsh ,你也可以使用=ls word,例如echo =lsls =ls来找出shell提到的ls

And you might also alias ls , or have a shell function ls masquarading the /bin/ls executable, etc.... Of course, programs could start /bin/ls without forking a shell (eg with plain fork + execve ...) 你也可以别名ls ,或者有一个shell函数ls masquarading /bin/ls可执行文件等....当然,程序可以启动/bin/ls 而不需要shell(例如使用普通的fork + execve ... )

Read also execvp(3) & environ(7) 另请阅读execvp(3)environ(7)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM