Macos – Command Line: z is installed but can’t find the command

command lineiterm2macosterminalzsh

I recently installed iTerm2 and ZSH (with oh-my-zsh) after some months of very light usage of the stock Terminal on Mac OS.

I heard of a tool called "z" and installed it via brew with brew install z. I used it for one night with no problems. Later on it stopped working, saying zsh: command not found: z. I tried installing it again, and was met with Warning: z-1.8 already installed.

How does this make sense? It's installed but the command is not found? How could I go about restoring this functionality?

Thanks!

Best Answer

One can inspect the list of files installed by a Homebrew formula via brew list -f <formula_name>. In this case, the output should like

> brew list -f z
/usr/local/Cellar/z/1.9/etc/profile.d/z.sh
/usr/local/Cellar/z/1.9/INSTALL_RECEIPT.json
/usr/local/Cellar/z/1.9/README
/usr/local/Cellar/z/1.9/share/man/man1/z.1

Note that in this case there's no command (and not even bin), just a z.sh. This makes sense because z is a shell tool, and have to be sourced into the shell as functions to get and set the shell environment; running as an external command simply doesn't offer deep enough integration. Therefore, you have to source z.sh into your shell, probably in .bash_profile, .bashrc, or .zshrc.

Usually, Homebrew formulae that require post-installation interactions in order to be usable will have instructions listed in caveats, which will be shown post-install, or manually retrieved via brew info <formula_name>. In this case,

> brew info z
<irrelevant info omitted>
==> Caveats
For Bash or Zsh, put something like this in your $HOME/.bashrc or $HOME/.zshrc:
  . `brew --prefix`/etc/profile.d/z.sh

Of course you should take that advise with a grain of salt, and use more modern and human-readable shell syntax:

source "$(brew --prefix)/etc/profile.d/z.sh"

Or

source /usr/local/etc/profile.d/z.sh

if you know your Homebrew installation is in /usr/local.

Related Question