简体   繁体   中英

Find path to binary without which

An Open Source project I contribute to has a dependency on which that I'd like to remove, as some Linux distros (like the cloud version of Fedora 20) don't ship with it by default, and I believe it's ugly to force its install, given how trivial our use of it is.

All we're doing is finding the path to the Java binary, then using that info to set $JAVA_HOME .

Is there a way to do that with Bash built-ins? In general, how can I somewhat-elegantly find a binary while minimizing dependencies (like which )? Or is it a better call to just use which , for example if the only alternative is to run find against directories in $PATH and the community believes that to be exceedingly inelegant?

Note that it's extremely ineffective to Google for which -related things.

POSIX-ly 正确方法:

cmd=$(command -v whatever)

Maybe by parsing the output of the shell builtin type ?

~/ type python
python is /usr/bin/python
~/ type type
type is a shell builtin

Tested with zsh , bash and sh

If you are using bash , the option -P gives you directly the result you want:

~/ type -P python
/usr/bin/python

My preference is already mentioned "type". But there is an additional command for this task:

 whereis -b <file>

locates binary for specified files

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