Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find path to binary without which

Tags:

linux

bash

shell

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.

like image 497
dfarrell07 Avatar asked Oct 21 '25 11:10

dfarrell07


2 Answers

The POSIX-ly correct way:

cmd=$(command -v whatever)
like image 157
Bruce K Avatar answered Oct 24 '25 02:10

Bruce K


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
like image 26
julienc Avatar answered Oct 24 '25 01:10

julienc