Home > How to Tips

How to Fix Mac Command Not Found Error in Terminal?

Updated on Friday, June 14, 2024

iBoysoft author Jenny Zeng

Written by

Jenny Zeng
Professional tech editor

Approved by

Jessica Shee

English Deutsch

Summary: This post explains why you see the "command“not found" error on Mac and 4 ways to get the problem fixed. We will guide you step by step with examples like "zsh: command not found: brew".

Fix Mac command not found

Sometimes, you may encounter the "command not found" error on Mac when trying to execute some commands in the Terminal window. 

Since Apple changed the default shell from bash to zsh in macOS Catalina, users running macOS Catalina or later will likely see something like the "zsh: command not found: brew" message. But users running macOS Mojave or earlier will likely see an error like "bash: brew: command not found."

When Terminal says "command not found", it means that the command you entered isn't in your search path. This can occur due to several reasons, as we will explain next.

Why does the 'command not found' error pop up on Mac's Terminal?

You can receive the Mac command not found error when one of the following happens:

  • The command was incorrectly entered.
  • The command isn't installed on your Mac.
  • The command was accidentally deleted or the system directory was modified or deleted.
  • The $PATH that specifies the directories that need to be searched for a command is incomplete, inaccurately set or cleared.

No matter what the reason is, you can follow the general guide below to fix the "command not found" error on Mac.

How to fix the Mac command not found error?

There are various cases regarding the "command not found" error on Mac. You can experience the "zsh: command not found" message on Mac Monterey or any other macOS version that uses zsh. The issue can also happen to any command. Here are some of the commonly reported error messages users received:

  • zsh: command not found: $
  • zsh: command not found: ls
  • zsh: command not found: brew
  • zsh: command not found: import
  • bash: brew: command not found
  • -bash: $: command not found
  • sudo: command not found
  • sudo: nano: command not found

Fortunately, these errors usually can be solved by the following solutions. It's recommended to try them one by one until the command can be executed successfully.

  • Fix 1: Check the command's syntax
  • Fix 2: Set $PATH as the default macOS path
  • Fix 3: Add your directory to PATH
  • Fix 4: Reinstall macOS or restore from Time Machine

Check the command's syntax

Users unfamiliar with the commands often make mistakes like forgetting to include a space at where it's required or adding unnecessary symbols into the command. For instance, you may enter sudo nano/private/etc/hosts to edit the Host files with Nano when the correct syntax has a space after nano, which should be sudo nano /private/etc/hosts.

Or you may copy and paste a line like $ brew install pyqt into Terminal, which only returns the error: "-bash: $: command not found" on Mac. That's because the $ is simply a sample Terminal prompt in the documentation that doesn't need to appear in the command.

So, the first thing you should check when encountering the "command not found" error on Mac's Terminal is the command's syntax. It's best to copy the original text into Terminal rather than type it yourself. Besides, comparing your command with other examples using the same command will also help.

Set $PATH as the default macOS path

A path refers to a specific location in a file system. Similarly, the PATH system variable ($PATH) for Terminal commands specifies several directories where the executable programs lie. With $PATH, your Mac knows where to search for executing a command, so you don't need to enter the absolute path of the command but the name only like ls.

However, if the path where the command you want to execute is located isn't set, you will get the "command not found" error on Mac, such as "zsh: command not found" or "zsh: command not found ls".

In which case, you need to first make sure the $PATH where commands reside is configured with the following steps:

  1. Open Terminal.
  2. Execute the following command to set the standard path that macOS uses in the command line:export PATH="/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"Set PATH system variables as the default macOS path
  3. Type in your password and hit Enter.

Try running the command you failed before again to see if the "zsh: command not found" error recurs in macOS Monterey. If the issue persists, proceed to the next fix.

 Add your directory to PATH

Another issue that may result in the "zsh: command not found" on M1 Mac is when the command you entered is not saved in the default $PATH. You can check the current default $PATH by executing the following command in Terminal: echo $PATH

Check the default directories Terminal seaching for running a command

The command will likely give you the default $PATH as /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin, which are the five directories your Mac uses with command lines.

If the command you use is stored in a directory excluded from the default ones, go through steps below to add it to the $PATH. For example, if you run into the "zsh: command not found: brew" error on Mac and you notice your Homebrew is stored at /opt/homebrew/bin, rather than the regular /usr/local/. Follow these solutions to fix it:

  1. Launch Terminal.
  2. Execute the following command to temporarily add the new directory (e.g. /opt/homebrew/bin) to $PATH:export PATH=$PATH:/opt/homebrew/bin
  3. Check whether the new directory is added with the echo command:echo $PATH

You can test if the command you want to use works now. If you want the change to the path to persist, you need to add it to the .zshrc file, .bash_profile or .bashrc file, depending on while shell you use. Follow these steps:

  1. Execute this command in Terminal to move into your home directory.cd
  2. Use the command below to list files in your home directory and check if you have a .zshrc file if you run zsh.ls -la
  3. If you don't have it, create one with the nano editor using this command. Otherwise, skip to step 4.touch .zshrc
  4. Run this command to edit .zshrc with nano.nano .zshrc
  5. Execute another command to add the new directory (e.g /opt/homebrew/bin)  to .zshrc.export PATH=$PATH:/opt/homebrew/bin
  6. Exit Terminal and reopen it.

Suppose this method isn't doing the trick, continue reading the next one to resolve the "zsh: command not found" error on your M1 Mac or Intel Mac.

Reinstall macOS or restore from Time Machine

If you were digging around in the system and happened to delete some system files or folders, you may have accidentally removed the files that are essential to Terminal commands. If that happens, you need to restore these system files.

You can choose to restore to a previous state if you have backed up your Mac with Time Machine. Otherwise, you can reinstall macOS without formatting the drive. Note that although reinstalling the operating system shouldn't cause any data loss, it's best to make a copy of the important files before acting.

Also read:

Createinstallmedia: Command Not Found (Sonoma/Earlier) Fixed