←back to thread

185 points thunderbong | 6 comments | | HN request time: 0.58s | source | bottom
1. lelandfe ◴[] No.43647389[source]
> Since I never remember which one is which, a good way to check is using the utility `file`: `file $(which useradd)`

While we're here, can someone explain why `which` prints some locations, and for others the whole darn file? Like `which npm` prints the location; `which nvm` prints the whole darn file.

replies(5): >>43647452 #>>43647455 #>>43647461 #>>43647472 #>>43648647 #
2. YardenZamir ◴[] No.43647452[source]
I can't say the reason, but i can note the pattern. If it's something in your path, like a program or a script which will show you where it is. If it's a shell function sourced you will see the whole thing.

If you write a function in your current session for example which will show you the content of that command. If you write that command in a file and put that file in your path which will show you where it is

3. awbraunstein ◴[] No.43647455[source]
`nvm` isn't a file, it is a bash function defined in some file (likely ~/.nvm/nvm.sh). So when you say `which nvm` it prints out the definition of the `nvm` function. This is setup when you added something like:

    export NVM_DIR="$HOME/.nvm"
    [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
    [ -s "$NVM_DIR/bash_completion" ] && \. 
 "$NVM_DIR/bash_completion"  # This loads nvm bash_completion
to your bashrc.
4. AlienRobot ◴[] No.43647461[source]
That sounds odd. Try using command -v instead?
5. cstrahan ◴[] No.43647472[source]
Are you sure that what is being printed is the contents of a file? And which shell are you using?

If your which command is a shell builtin, and nvm is a function, then you’re likely seeing the content of that function.

6. adrianmonk ◴[] No.43648647[source]
For this situation, in bash, use 'type nvm' (instead of 'which nvm' or 'file nvm'). It will tell you what 'nvm' is (executable, shell alias, shell function, shell builtin, etc.), which will probably solve the mystery.