[shell] commands not found on zsh

I am using the z Shell (zsh) instead of the default bash, and something wrong happen so that all commands who used to work are no longer recognized:

ls
zsh: command not found: ls

open -e .zshrc
zsh: correct 'open' to '_open' [nyae]? 

I don't know how to reset zsh or how to fix this. Here is the content of $PATH variable:

echo $PATH
/Users/Malloc/bin/Sencha/Cmd/3.1.2.342:/usr/local/bin/mate

I cannot open the .bash_profile file nor .zshrc file, seems the only solution is to reset the zsh. Any ideas?

EDIT:

I even tried to reset zsh as indicated in this thread, but always got command not found error:

exec zsh
zsh: command not found: zsh

so what's going on? why all commands are lost?

This question is related to shell osx-lion zsh

The answer is


If you like me, you will have two terminals app, one is the default terminal with bash as the default shell and another iTerm 2 with zsh as its shell. To have both commands and zsh in iTerm 2 from bash, you need to do the following:

On iTerm 2, go to preferences (or command ,). Then go to the profile tab and go down to command. As you can see on the picture below, you need to select command option and paste path of zsh shell (to find the path, you can do which zsh).

enter image description here

At this point you will have the zsh as your default shell ONLY for iTerm 2 and you will have bash as the global default shell on default mac terminal app. Next, we are still missing the commands from bash in zsh. So to do this, you need to go on your bash (where you have your commands working) and get the PATH variable from env (use this command to do that: env | grep PATH).

Once you have that, go to your iTerm 2 and paste your path on "send text at start" option.

export PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin && clear

Just reopen iTerm 2 and we are done!


  1. Use a good text editor like VS Code and open your .zshrc file (should be in your home directory. if you don't see it, be sure to right-click in the file folder when opening and choose option to 'show hidden files').

  2. find where it states: export PATH=a-bunch-of-paths-separated-by-colons:

  3. insert this at the end of the line, before the end-quote: :$HOME/.local/bin

And it should work for you.

You can test if this will work first by typing this in your terminal first: export PATH=$HOME/.local/bin:$PATH

If the error disappears after you type this into the terminal and your terminal functions normally, the above solution will work. If it doesn't, you'll have to find the folder where your reference error is located (the thing not found), and replace the PATH above with the PATH-TO-THAT-FOLDER.


You can create a symlink in /usr/local/bin/

sudo ln -s $HOME/.composer/vendor/bin/homestead /usr/local/bin/homestead

Restarting the terminal also made the trick for me.


I fixed the issue by opening the terminal preference general tab and changing the Command (complete path) to /bin/bash to default and then editing the ~/.zshrc file.

enter image description here

export PATH="all your path inside the quotes"

...without any whitespace between the PATH=" and save the file.

enter image description here

After saving the file, change the /bin/zsh in your command or select default enter image description here

...and restart terminal and you should have your zsh shell working again!


Best solution work for me for permanent change path

Open Finder-> go to folder /Users/ /usr/local/bin

open .zshrc with TextEdit

.zshrc is hidden file so unhide it by command+shift+. press

delete file content and type

export PATH=~/usr/bin:/bin:/usr/sbin:/sbin:$PATH

and save

now

zsh: command not found Gone


In my case, it was by mistake the commented code was uncommented at the very first line.

# If you come from bash you might have to change your $PATH.

The hash was removed. After adding it back worked fine.

I just posted this here so someone might find this helpful. Because it's just start of the line and might be deleted by mistake like I had done. And seems there's no solution after trying several ways.


As others have said, simply restarting the terminal after you've made changes should reset and changes you've made to your ~/.zshrc file. For instance after adding function to open visual studio:

function code {  
    if [[ $# = 0 ]]
    then
        open -a "Visual Studio Code"
    else
        local argPath="$1"
        [[ $1 = /* ]] && argPath="$1" || argPath="$PWD/${1#./}"
        open -a "Visual Studio Code" "$argPath"
    fi
}

I was able to use the keyword code to open the program from the command line.


Uninstall and reinstall zsh worked for me:

sudo yum remove zsh
sudo yum install -y zsh

In my case I was using the variable path in lowercase!

So in /etc/profile.d I was running a script that made use of the variable path. Because it was in lowercase I never thought it could mess up with the actual variable PATH. Be careful and do not use the variable path on your scripts.


if you are using macOS, try to follow this step

if you write the code to export PATH in ~/.bash_profile then don't miss the Step 1

Step 1:

  • make sure .bash_profile is loaded when your terminal is an open, check on your ~/.bashrc or ~/.zshrc (if you are using zsh), is there any code similar source ~/.bash_profile or not?. if not you can add manually with adding code source ~/.bash_profile in there
  • Also make sure this code is on your .bash_profile > export PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin if it not in there, add that code into it

Sep 2:

  • make sure the "Visual Studio Code.app" is in the right place > "/Applications" or "/Users/$(whoami)/Applications"
  • remove the old installed vs-code PATH rm -rf /usr/local/bin/code
  • open "Visual Studio Code.app"
  • CMD+Shift+P and then select "Shell Command: Instal "code" command in PATH"
  • restart your Mac and check by run this code -v, it should be work

In your ~/.zsh config file include the path to your bash path file that contains your aliases. In my case it was including the line "source ~/.bash_profile" inside of ~/.zsh .


My solution:

Change back to bash:

source .bashrc

next:

echo $PATH

copy this:

/home/frank/.asdf/shims:/home/frank/....

back to the zsh:

source .zsh

open .zshrc:

and paste:

 export PATH=/home/frank/.asdf/shims:/home/frank/....

restart terminal


For me just restarting my terminal seemed to fix the issue.


I had a similiar problem when I installed ZSH on my ubuntu, I was getting the error zsh: command not found node when I tried to exec node or another command.

The solution for my was to open my ~/.szhrc file and write the next code on it:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

Then I closed and reopened my terminal and that's it.


A way to edit the .zshrc file without doing it through iTerm2 or native Terminal on macOS is to use a terminal in another application. For example, I used the terminal as part of VSCode and was able to find and edit the file.


Examples related to shell

Comparing a variable with a string python not working when redirecting from bash script Get first line of a shell command's output How to run shell script file using nodejs? Run bash command on jenkins pipeline Way to create multiline comments in Bash? How to do multiline shell script in Ansible How to check if a file exists in a shell script How to check if an environment variable exists and get its value? Curl to return http status code along with the response docker entrypoint running bash script gets "permission denied"

Examples related to osx-lion

commands not found on zsh Why does cURL return error "(23) Failed writing body"? What is the "Illegal Instruction: 4" error and why does "-mmacosx-version-min=10.x" fix it? Location of the mongodb database on mac How do I timestamp every ping result? How to run iPhone emulator WITHOUT starting Xcode? configure: error: C compiler cannot create executables sudo: port: command not found Disable elastic scrolling in Safari Completely uninstall PostgreSQL 9.0.4 from Mac OSX Lion?

Examples related to zsh

Conda command not found Zsh: Conda/Pip installs command not found commands not found on zsh How do I update zsh to the latest version? npm global path prefix How to permanently set $PATH on Linux/Unix? zsh compinit: insecure directories Command not found after npm install in zsh Adding a new entry to the PATH variable in ZSH Where to place $PATH variable assertions in zsh?