Looking for some help writing a bash script that checks if a particular bundle id is installed and proceeds to install the package if not found.

Found that we can use "mdfind kMDItemCFBundleIdentifier = "com.hnc.Discord" (for example) returns the location for the particular bundleid if the app is not found nothing comes back. Maybe I can use this with an if statement somehow?

Any help would be much appreciated.

1 Answer 1


mdfind does not produce an unsuccessful exit code but you could pipe the results to grep which does. The code would look something like this:

mdfind "kMDItemCFBundleIdentifier = com.apple.Terminal" | grep -q Terminal || installer ....

|| is logical or.

It could also be written

if mdfind "kMDItemCFBundleIdentifier = com.apple.Terminal" | grep -q Terminal
  installer ...
  • That would work! Any way we can make the grep universal instead of a specific term? The bundle id will end up being a variable. May 3, 2018 at 19:16
  • 1
    @WebsterMaster What do you mean with universal? You can use a shell variable instead of com.apple.Terminal, also [[ $(mdfind "kMDItemCFBundleIdentifier = $BUNDLEID" | wc -l) -eq 0 ]] will avoid the grep part. Or use ... -gt 0 and run the installer in the then part.
    – nohillside
    May 4, 2018 at 11:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .