简体   繁体   中英

which command doesn't work on my computer

In the past, we 'which' command to get the info of the relevant software on our computer, Like:

which python
which git 

But now it seems don't work on my MacOS Mojave. Is there anything wrong with my setting?

New edition: The result will turn out to be like this

AA:~ AA$ which python
/usr/bin/which: illegal option -- -
usage: which [-as] program ...

New edition2:

AA:~ AA$ type --all which
which is aliased to `alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
which is /usr/bin/which

AA:~ AA$ type -all python
python is /Users/AA/anaconda3/bin/python
python is /Users/AA/anaconda/bin/python
python is /usr/bin/python
AA:~ AA$

The alias is what causes the error message. Apparently the alias definition is simply erroneous for your system. It's not clear what defined this alias or why; it seems wrong on several levels, so I doubt it's part of the standard install.

You can remove the alias with unalias which ; but I'm guessing it's defined in one of your startup files, and should be removed from there - after this point, it should be gone for good the next time you log in.

But anyway, you should not be using which - it's better to accustom yourself to the POSIX standard command type . It was introduced specifically to replace which but it's apparently still hard to eradicate the old command from people's minds.

For me, this was when something from a yarn global upgrade replaced my system's /usr/bin/which .

/usr/bin/which -> ../local/share/.config/yarn/global/node_modules/.bin/which

Since your error message is exactly the same as the text strings inside that program, it is possible that your issue is from a similar source.

console.error('which: illegal option -- ' + flag)
console.error('usage: which [-as] program ...')

"Fixed" with reinstalling which through the system's package manager (eg sudo dnf reinstall which ). That may now interfere with whatever JavaScript package depended on that, but I plan on removing those globals, so I will not find out.

Better fix: I feel like my system is dirty now and needs all of its bin files checked... Comparing everything in /usr/local/share/.config/yarn/global/node_modules/.bin to see if it has an equivalent in /usr/bin seems like an easy enough way to look for other points of interference.

for x in $(ls /usr/local/share/.config/yarn/global/node_modules/.bin/*); do
    ls -l /usr/bin/"$(basename "${x}")"
done

Then those can be checked against the system's package manager with commands like the following. Then reinstalled if needed.

sudo dnf info ...
sudo dnf provides ...

More:

Seems weird to me that it would allow clobbering existing system programs. At the very least, I would expect it to have used /usr/local/bin instead. That extra system and system-like package management seems to be why they have done away with yarn global in yarn 2 (berry).

I have different packages for sudo yarn global list and yarn global list . Maybe using sudo at some point was my mistake?


PS Thanks goes to @ tripleee , I was unaware of type . I have seen various other ways of attempting to handle different which programs and different versions of which , along with alternatives like test (and others I have forgotten), but type looks worth trying as a replacement.

PPS Annoying to find out that this OS is configured to run tab completion through which.

PPPS Yes, I know scripting with the output ls is a bad habit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM