How to Fix Terminal Closing Immediately on macOS

itermterminal

I accidentally deleted some files from my Applications directory. I don't recall exactly what but some of them included the Cellar directory.

Now, each time I try to open the terminal, it opens the terminal window but it immediately closes (i.e., less than one second).
Since, I have no operational terminal, I don't know how to solve the problem of re-installing the terminal or applying any fix.
I tried downloading iTerm and installing it but the exact same thing happens to it: the moment I open it, it closes.

Any ideas?

===Update===
Actually tried again, and this was the message that I got. When I click Ok button, the terminal closes immediately.
enter image description here

Best Answer

Your default shell is the brew-installed zsh. You probably removed the folder containing zsh and some other important files/folders/libraries.

In the preferences of Terminal (or System Preferences > Users & Groups > ctrl-click user_name > Advanced Options... > login shell | as far as I remember you have to reboot to make this work) set the default shell back to /bin/bash. Then relaunch Terminal and uninstall brew and reinstall it afterwards. You may have to uninstall brew manually by deleting some folders in /usr/local/ if you deleted too many files and folders previously ("I accidentally deleted some files from my Applications directory. I don't recall exactly what but some of them included the Cellar directory.").

In a second step reinstall brew and brew installed execs.