zsh: command not found [fix] macOS

zsh command not found
zsh command not found
zsh: command not found command-name

This is the most common error when you execute a command on the zsh shell if,

  1. Executed command is invalid.
  2. Executed command is misspelled.
  3. The command is valid but command package not installed.
  4. The command package is installed not set in $PATH environment.
  5. The command path is not set in the .zshrc file.

Let's see some examples of each of them,

Example 1: Executed command is invalid

% cls
zsh: command not found: cls

As you may have noticed cls is not a command to clear the screen on a Unix/macOS/Linux based Operating System. The correct command is clear, hence the command not found here is due to an invalid command.

Example 2: Executed command is misspelled

% gerp 'myfile.txt'
zsh: command not found: gerp

The command is misspelled, the correct spelling for the command is grep

Example 3: The command is valid but command package not installed

% brew
zsh: command not found: brew

brew is a valid command, but here the problem is that the package brew is not installed. In order for this command to work you need to install the package first.

How to fix: brew command not found.

Example 4: The command package is installed not set in $PATH environment.

% mvn
zsh: command not found: mvn

If you have an external package installed, it needs to set in the $PATH environment variable. If you do not do that and run the command from location away from the package installation you will again get command not found error.

Article: How to set $PATH variable

Example 5: The command path is not set in the .zshrc file.

% brew
zsh: command not found: brew

You must have noticed that as soon as you exit your Mac Terminal, the environment variable such as $PATH is not available. The $PATH was available until the Terminal session was active, but soon as it was terminated, we do not have access to the variable. In order to set the $PATH variable permanently, you would need to set it in the .zshrc file (Z-Shell resource file).

How to add $PATH in .zshrc file?
  1. Open Terminal,
  2. Make sure you are on Zsh Shell, if not type zsh and hit enter,
  3. Now type nano ~/.zshrc
  4. Now add your $PATH variable details: Example: export PATH="$PATH:/opt/homebrew/bin/"
  5. Press Control + X, followed by Y to save the file, press enter to exit Nano,
  6. Now type command source ~/.zshrc to apply changes.

Have Questions? Post them here!

Recent Posts:

Code2care is an initiative to publish and share varied knowledge in programming and technical areas gathered during day-to-day learnings and development activities.

Students and Software Developers can leverage this portal to find solutions to their various queries without re-inventing the wheel by referring to our easy to understand posts. Technical posts might include Learnings, Video Tutorials, Code Snippets, How Tos, Blogs, Articles, etc.