"which" command doesn't work / path of Safari?

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.


Solution 1:

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.

Solution 2:

This is working as designed.

which is a command line executable that looks on your path (and other parts of shell environment) to find things you can start from the command line

Applications like Safari (anything bundled as an application which will be a directory ending in .app with certain files in specific sub directories) are not launched from the command line. They are launched from the desktop and not via any shell commands.

You can however launch applications from the command line using the command open. man open gives details. The example to launch Safari is open -a Safari

Note that if you are trying to open Safari when you login putting this command in your shell files will not work as the shell is not run until you launch Terminal.app.
The easiest way to launch Safari at login is to run the app from the desktop, right click on the app's icon in the dock and choose Options then Open at Login.
There is also a list of things that are launched at login for each user in System Preferences->Users & Groups and this can be edited there.

Solution 3:

On macOS applications aren‘t part of the standard PATH and can‘t be started by calling them from the command line (well, yes, they can, but it‘s not so easy). But you can use

open foo.html

or

open -a Safari

to start Safari from bash.

Solution 4:

Add following in your .bash_profile.

alias safari="/Applications/Safari.app/Contents/MacOS/Safari"  

You can now start safari in your Terminal. As mentioned before, .app are bundles and cannot start directly from Terminal.

To Edit your .bash_profile:

  1. Type in Terminal nano .bash_profile (make sure your are in your home-directory test it with the pwd command. Usually /Users/Username.

  2. add the alias Line above at the end of the document.

  3. type CTRL-O to save the file
  4. type CTRL-X to exit nano
  5. type source .bash_profile to update
  6. type safari to start Safari.

You can do this with most of the applications in the /Application-Folder.