What is equivalent to "which" for commands that are not related to executable files?

I am trying to figure out how a specific command is defined. I checked all the locations of $PATH and could not find the file called as my command, so it seems to be something else.

Here is an example of using nvm that is not executable:

 me@MacBook :~$ which cat /bin/cat me@MacBook :~$ which nvm me@MacBook :~$ nvm --version 0.33.8 

which nvm just returns nothing.

What is equivalent to β€œwhich” for commands like this on UNIX-based systems?

+5
source share
2 answers

The command you are looking for is type .

type nvm will show how the interpreter interprets the command, so unlike which it will display aliases, functions, and unreleased paths.

+6
source

Here is the answer to a similar question, which advises not to use which for reasons not related to the point in question.

However, your assumption that which can only see executable files is incorrect.

However, it does not see the default functions and aliases.

This is why the manpage says:

The recommended way to use this utility is to add an alias function (C shell) or a shell (Bourne shell), for which, as shown below:

  [ba]sh: which () { (alias; declare -f) | /usr/bin/which --tty-only --read-alias --read-functions --show-tilde --show-dot $@ } export -f which 

If you define this function in your .bashrc and reuse it, you should be able to do

 which -a 

and it should also give you functions and aliases.

However, note: if some profile or bashrc has already defined something for which , this takes precedence (you can find it with type -a which btw).

If I define a script, a function, and an alias called something , I get with type -a :

 type -a something something is aliased to `echo "something"' something is a function something () { echo "function something" } something is /home/myself/bin/something 

So far, which -a after creating the function gives me:

 which -a something alias something='echo "something"' /usr/bin/echo /bin/echo something () { echo "function something" } ~/bin/something 
+3
source

Source: https://habr.com/ru/post/1275762/


All Articles