Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to know from where the command ran in linux

Tags:

linux

bash

unix

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?

like image 457
Gaurav Khurana Avatar asked Dec 25 '22 00:12

Gaurav Khurana


1 Answers

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.

like image 179
kojiro Avatar answered Jan 06 '23 13:01

kojiro