lukechilds / zsh-nvm

Zsh plugin for installing, updating and loading nvm
MIT License
2.24k stars 113 forks source link

Avoid printing on console when no binary found. #22

Closed pedroxs closed 7 years ago

pedroxs commented 7 years ago

was getting /usr/bin/which: no node in (/usr... on every new session opened.

lukechilds commented 7 years ago

Thanks for taking the time to submit this.

Strange, I don't have the same issue, what version of zsh are you running?

I'm on zsh 5.3.1 (x86_64-apple-darwin15.6.0) and I get:

$ which nonexistentbin
nonexistentbin not found

$ which nonexistentbin 2>/dev/null
nonexistentbin not found

$ "$(which nonexistentbin)"
zsh: command not found: nonexistentbin not found

$ [[ "$(which nonexistentbin)" ]]

$ "$(which nonexistentbin 2>/dev/null)"
zsh: command not found: nonexistentbin not found

$ [[ "$(which nonexistentbin 2>/dev/null)" ]]

So for me, running inside test [[ ]] suppresses the output but piping stderr to /dev/null makes no difference.

lukechilds commented 7 years ago

I've just noticed you're using /usr/bin/which, this is a zsh specific plugin and expects to use the zsh builtin which.

Is there a specific reason you're using your system which over the zsh builtin? If not, you should consider removing it.

More info here: http://unix.stackexchange.com/a/63782

pedroxs commented 7 years ago

Interesting information, my version is zsh 5.2 (x86_64-redhat-linux-gnu) and I get the following outputs:

~❯ which nonexistentbin
/usr/bin/which: no nonexistentbin in (/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin)
~❯ which nonexistentbin 2>/dev/null
~❯ "$(which nonexistentbin)"
/usr/bin/which: no nonexistentbin in (/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin)
zsh: permission denied: 
~❯ [[ "$(which nonexistentbin)" ]]
/usr/bin/which: no nonexistentbin in (/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin)
~❯ "$(which nonexistentbin 2>/dev/null)"
zsh: permission denied: 
~❯ [[ "$(which nonexistentbin 2>/dev/null)" ]]

I am on Fedora 25 and searching for where the which command was aliased I found the following file /etc/profile.d/which2.sh with contents:

# Initialization script for bash and sh

alias which='(alias; declare -f) | /usr/bin/which --tty-only --read-alias --read-functions --show-tilde --show-dot'

If I run the above commands with the builtin zsh command I get similar results as yours:

~❯ unalias which
~❯ which nonexistentbin
nonexistentbin not found
~❯ which nonexistentbin 2>/dev/null
nonexistentbin not found
~❯ "$(which nonexistentbin)"
zsh: nonexistentbin not found: command not found...
zsh: command not found: nonexistentbin not found
~❯ [[ "$(which nonexistentbin)" ]]
~❯ "$(which nonexistentbin 2>/dev/null)"
zsh: nonexistentbin not found: command not found...
zsh: command not found: nonexistentbin not found
~❯ [[ "$(which nonexistentbin 2>/dev/null)" ]]

I guess you don't need to merge this, maybe add some documentation on the README for future reference, I will just remove the offending alias to which.

Thanks for the help!

lukechilds commented 7 years ago

No problem, glad you got it sorted 👍