Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I determine if a binary is installed using bash?

Tags:

bash

Using Terminal, what's the easiest way to determine if a binary named dcmdump is installed on the user's system? If installed I need to know its location (e.g. /usr/local/bin) and if its not installed then I'd like the Terminal to echo FALSE.

I know very little Terminal script but typing:

command -v dcmdump

Outputs the directory dcmdump is installed in (if installed - which is great) but nothing is echoed if its not (I want it to echo the string FALSE)

like image 677
Garry Pettet Avatar asked May 09 '26 04:05

Garry Pettet


1 Answers

You can use this:

$ which dcmdump 2>/dev/null || echo FALSE

Here is how it works:

  • The entire expression is a boolean OR (written as ||) of two commands. The left-hand-side of the OR is the command which dcmdump 2>/dev/null, the right-hand-side is echo FALSE.
  • Thanks to lazy evaluation (or short-circuiting), if the left-hand-side evaluates to “true” (or “success”), then the right-hand-side will not be evaluated. Otherwise, it will.
  • The command which NAME looks for an executable named NAME in your current shell $PATH. If it finds one, it prints its absolute path to the standard output and exits with a status indicating “success”, otherwise, it might or might not print an error message to the standard error output and returns a status indicating “failure”.
  • Since we don't want which's error message but our own, we redirect which's standard error output to the black hole /dev/null with the 2>/dev/null part.
  • The command echo TEXT simply outputs TEXT to the standard output. If you wanted FALSE to be printed to the standard error output, you could redirect it using echo FALSE >&2.
like image 137
5gon12eder Avatar answered May 11 '26 08:05

5gon12eder



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!