Suppose i ran a command in unix like ls
or rm
and path variable is set as below
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
(just an example) may be there in /sbin
as well /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. is there a direct way to search ls
from where it ran?
When you run an external command in bash, bash hashes that command to avoid having to do the path lookup twice. The hash
command can tell you which command was run. 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.
hash
tells you what path/command was used/hashed. If your PATH or filesystem changes during hash
's lifetime, hash
can tell you about commands that happened before that change.which
is an external command that looks up a command in the PATH
environment variable.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.See help hash
in your bash to read more about how it works.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With