in mac always getting zsh: command not found: [closed]
Asked Answered
P

23

342

I am using the z Shell (zsh) instead of the default bash, and something wrong happen so that all commands that 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?

For reset shell i just add this in .zshrc and working fine

eval "$(rbenv init -)"
export PATH="$PATH:$HOME/.rvm/bin"
export PATH="/opt/homebrew/bin:$PATH"
export PATH="/usr/local/opt/ruby/bin:/usr/local/lib/ruby/gems/2.6.3p62/bin:$PATH"
Pola answered 25/8, 2013 at 11:12 Comment(3)
Shortcut: #20107083 (latest answer)Mesne
I got an Apple M1 processor and for me using "nano .zshrc" command I was able to edit the file.Presa
run this command-> export PATH=$PATH:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbinTintinnabulation
G
578

It's evident that you've managed to mess up your PATH variable. (Your current PATH doesn't contain any location where common utilities are located.)

Try:

PATH=/bin:/usr/bin:/usr/local/bin:/sbin:${PATH}
export PATH

Alternatively, for "resetting" zsh, specify the complete path to the shell:

exec /bin/zsh

or

exec /usr/bin/zsh
Gilli answered 25/8, 2013 at 12:4 Comment(8)
When I upgraded to Catalina I switched to the zsh shell and it messed up my paths to the point where I couldn't execute any third party apps I use. I just had to modify the .zshrc file and add "export PATH=<<location of my bins>>" and did the exec /bin/zsh and all was good.Floatation
@Gilli @Robert Walters I try your answer, but I still get zsh command not found when I try to access anything, is there another way solve thisPamulapan
@uyhaW FWIW, I just ran the bash command in my zshell environment, (so it switched to bash) opened my .bash_profile...then quit the terminal. restarted in zsh and it was suddenly working for me. IDK.Glib
add export PATH=/bin:/usr/bin:/usr/local/bin:${PATH} begging of ~/.zshrc file, after save file run source ~/.zshrcClichy
in Monterey this not works. Works with this command: export PATH="/usr/local/bin:/usr/bin:bin:/usr/sbin:/sbin". Hours and hours searching resolutions, I find. :)Boorish
zsh looks for .zprofile, so just create this file under your username and write your path or script you are trying to add -- basically create this by vim ~/.zprofile - under your userVerile
To me this happened when I was using a script doing for path in (...); do something with $path; done. This worked perfectly in bash but zsh is apparently case insensitive when it comes to variable names, so $path messed up $PATH.Tafilelt
If npm's default directory has manually been changed, to resolve EACCES permissions errors when installing packages globally (docs.npmjs.com/…), you should add in your .zshrc file, on the export PATH line : $HOME/.npm-global/binBarometrograph
M
60

On MacOS BigSur Click Terminal >> Preferences change the from /bin/zsh to /bin/bash Close and reopen the terminal

FROM Preference screen before

TO Preference screen after

You can now go back to .zshrc file to revert the last change you made

Marna answered 24/3, 2021 at 16:7 Comment(4)
Still get same issue except -bashSuzy
This solution was exactly the one I needed. Thanks!Unearned
This worked in my Mac with M2 processor.Bolan
Great, its working for me. I have added wrong path in zshrc fil. Just removed the path and its working fine.Triplane
A
30

The best solution works for me for a permanent change path.

Open Finder -> go to folder /Users/<your user name here> /usr/local/bin

open .zshrc with TextEdit:

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

delete file content and type:

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

and save.

now zsh: command not found is gone.

Argenteuil answered 3/7, 2020 at 7:40 Comment(1)
if we open with TextEdit , mac will show following warning -> You don’t own the file “.zshrc” and don’t have permission to write to it. So i open .zshrc file with VSCode and save as sudo.Snowshed
I
27

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 .

Inextirpable answered 4/10, 2016 at 16:15 Comment(2)
I don't have a ~/.zsh file. Instead I had to put it in /etc/zshrc.Despotism
Same but mines in file: ~/.zshrcEustoliaeutectic
J
25
  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.

Jeneejenei answered 24/10, 2014 at 16:41 Comment(1)
Aha, therein lies my mistake which led zsh to apparently "ignore" my PATH setting. From bash, I was used to prefixing directories inside my home folder with ~. So it turns out for zsh I should instead be using the $HOME variable, like in your example.Trainee
B
16

I solved my problem by reinstalling and/or add at the end of the ~/.zshrc file:

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

Oh My Zsh is installed by running one of the following commands in your terminal. You can install this via the command-line with either curl or wget.

curl:

sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

wget:

sh -c "$(wget https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh -O -)"
Bonita answered 20/4, 2021 at 19:25 Comment(0)
D
14

Works on macOS 12 Monterey:

You can manually edit .zshrc file and revert the changes you made to PATH.

  • Open TextEdit app.

  • Go to folder: cmd + shift + g

  • type: ~/

  • See hidden files: cmd + shift + .

  • select: .zshrc

  • Revert changes, save file, and reopen terminal.

Denudate answered 26/12, 2021 at 12:11 Comment(0)
U
12

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

Uredium answered 15/3, 2020 at 20:34 Comment(1)
In my case I had to reshim the asdf python install asdf reshim python and then it worked!Ortego
S
10

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!

Sunward answered 15/3, 2016 at 2:48 Comment(0)
S
9

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

Sallet answered 16/7, 2014 at 15:16 Comment(0)
I
9

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

sudo ln -s $HOME/.composer/vendor/bin/homestead /usr/local/bin/homestead
Isolating answered 5/8, 2016 at 12:40 Comment(2)
Thanks a lot. I was having problems and this answer made me realize that I was not using $HOME and providing a relative path. Using full path with $HOME fixed my problem.Oatmeal
+1 for many simple use cases: Adding a symlink is the way to go if you're just looking to add one executable. If you're looking to manage multiple executables in a new folder, then adding on to $PATH is better in that use case.Schoolboy
A
6

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!

Alunite answered 25/7, 2018 at 18:32 Comment(1)
The reason why we added the export to "Send text at start" is that for someone reason for me, the path always reset to what it was before whenever I reopened iTerm 2. So this is a nice way of fixing it.Alunite
D
3

To permanently fix it, you shall go to .zshrc file which is in your root directory. You can do:

  1. cd to return to your root directory
  2. code . to open directory from your VS Code to be able to easily edit your .zshrc file
  3. click on .zshrc file from your VS Code
  4. (1) If you have not made any changes to this file previously, It shall look as follows:

eval "$(rbenv init -)"

export PATH="$PATH:$HOME/.rvm/bin"

export PATH="/opt/homebrew/bin:$PATH"

OR

(2) If you did make any change, like adding some new PATH, then simply remove exactly that PATH. In my case, I used to have a homebrew path as well, so I deleted it. So, now it looks the same as in the step 4 (1).

Demodena answered 29/10, 2021 at 14:32 Comment(0)
O
3

For macOS, had to add source $HOME/.bash_profile to .zshrc file.

  1. $ code ~/.zshrc (Opens .zshrc file on VSCode)
  2. Add source $HOME/.bash_profile on top of the file and save it.
  3. $ source ~/.zshrc to refresh .zshrc.

Don't forget to refresh.

Okapi answered 3/1, 2022 at 17:47 Comment(1)
This is wacky; .bash_profile should contain commands specific to Bash, and may have syntax which isn't compatible with Zsh (depending on how you have configured it).Gargoyle
M
2

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.

Maighdiln answered 14/3, 2019 at 17:17 Comment(0)
R
1

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 ~/.zshrc 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.

Raseta answered 31/1, 2021 at 21:30 Comment(0)
C
1

You need to create the .zprofile file and write into it

cd /
touch .zprofile
vim .zprofile
//Put Your Path

exit and Save and after run this code into shell

 source ~/.zshrc
Christen answered 9/3, 2021 at 18:27 Comment(0)
C
0

Restarting the terminal also made the trick for me.

Carlottacarlovingian answered 6/12, 2015 at 17:11 Comment(0)
M
0

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
Manassas answered 2/11, 2020 at 8:14 Comment(0)
G
0

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.

Geosyncline answered 14/11, 2020 at 17:38 Comment(0)
O
-1

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.

Oriane answered 31/3, 2016 at 18:46 Comment(0)
C
-1

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.

Companionway answered 19/9, 2018 at 21:44 Comment(0)
L
-3

Uninstall and reinstall zsh worked for me:

sudo yum remove zsh
sudo yum install -y zsh
Loughlin answered 18/5, 2018 at 20:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.