macOS Terminal – Fix ‘which’ Command Not Working for Safari Path

command linemacospathsafariterminal

Why can’t I locate programs like Chrome or Safari or FireFox with the which command line tool?

pasocon:~ ThisUser$ which python
/anaconda3/bin/python
pasocon:~ ThisUser$ which firefox
pasocon:~ ThisUser$ which Safari
pasocon:~ ThisUser$ which chrome
pasocon:~ ThisUser$ 

This is what I get with the which command. It is not giving me the path for many applications. Am I making a mistake here or is this potentially an issue to be fixed?

Alternatively, I was just looking for the path of Safari (so that you can open Safari by directing to this path in a configuration file). I am sure this is basic knowledge but I've somehow failed to find it.

Best Answer

which searches for binaries in the $PATH, a.k.a. command-line tools. User applications are not such tools and not available in the command line.

While Safari.app is an application, the Safari binary is not usually added to the $PATH because it is not a command line tool — if you just run Safari in Terminal your shell will inform you that the command cannot be found.

To find the path to an application, you can use lsregister, which is a tool for adding and querying the Launch Services database, used by macOS in part to find applications.

/System/Library/Frameworks/CoreServices.framework/\
  Versions/A/Frameworks/LaunchServices.framework/\
  Versions/A/Support/lsregister

You can -dump the database and filter with grep.

/System/L*/Fr*/CoreSe*/V*/A/F*/L*/V*/A/S*/lsregister -dump |
  grep -ie "path:.*safari"
    path:          /Applications/Safari.app

This will return every instance of Safari registered with Launch Services, which will include backups. If you just want to open Safari, you shouldn't be dealing with getting paths and handling it yourself; instead, get macOS to do that for you: open -a Safari.