How to Use Bash on macOS: How to Switch from Zsh?
In our articles so far, we've learned everything from the basics of Bash to installing it on Windows with WSL. This time, we're going to do a deep dive for our macOS users to make your terminal environment even more comfortable. If you're a web creator who loves macOS, you may have opened the Terminal and been puzzled by the unfamiliar "zsh" text, wondering, "Wait, isn't this Bash?"
That's right—since macOS Catalina (released in 2019), the default shell on Mac has been changed from Bash to Zsh (Z Shell). While Zsh is a highly functional and excellent shell, many tutorials and servers around the world are still based on Bash. For this reason, many people want to set up a Bash environment for learning purposes or to work in a shell identical to their server.
In this article, we will clearly explain the following points for all you macOS users out there:
- What are the basic differences between Zsh and Bash?
- How to safely install the latest version of Bash on macOS.
- How to switch your default shell from Zsh to Bash (and back again) whenever you want.
By the time you finish this article, you'll have the knowledge to freely customize your macOS terminal environment and choose the optimal shell for each project!
Zsh vs. Bash: What's the Difference?
First off, both Zsh and Bash are a type of "shell," which means they are interactive programs that we use to give commands to the OS. Many of the basic commands (like ls, cd, mkdir, etc.) can be used in exactly the same way in both shells.
So, why did Apple switch from Bash, which was the standard for many years, to Zsh? Let's look at the main differences and reasons.
- Bash (Bourne-Again SHell): The long-reigning "king of shells," which has been the standard on most Linux systems and older versions of macOS. It's known for its stability, compatibility, and the vast amount of information available for it.
- Zsh (Z Shell): A more modern and highly functional shell developed as an extension of Bash. Its appeal lies in features like powerful completion (suggesting commands), spell correction, and extensive theme customization.
- Background of the switch: One of the major reasons Apple switched to Zsh is related to licensing. Newer versions of Bash (v4.0 and later) use the GPLv3 license, and Apple wanted to avoid its terms. As a result, the version of Bash included with macOS has remained a very old version 3.2. On the other hand, Zsh uses a more permissive license, which allowed Apple to include the latest version.
In conclusion, Zsh is almost a superset of Bash, but it's not 100% compatible. The need to use Bash to match server environments or existing scripts is still very much alive and well.
Step 1: Install the Latest Version of Bash
As mentioned, the version of Bash that comes standard with macOS is very old. First, let's install the latest version of Bash using Homebrew. Homebrew is a convenient package manager for macOS.
Open the Terminal and run the following command to install Bash. (If you haven't installed Homebrew itself yet, please do that first.)
brew install bash
This will install a new version of Bash managed by Homebrew, separate from the standard macOS /bin/bash. (It's usually installed at /usr/local/bin/bash or /opt/homebrew/bin/bash).
Version Check
After installation, let's compare the versions of the old and new Bash. First, check the version of the old, standard macOS Bash.
$ /bin/bash --version
GNU bash, version 3.2.57(1)-release ...
Next, check the version of the new Bash installed with Homebrew. (Please adjust the path to match your environment.)
$ /opt/homebrew/bin/bash --version
GNU bash, version 5.2.15(1)-release ...
As you can see, a new Bash version 5.x has been installed!
Step 2: Switching Your Default Shell to Bash
Just installing the new Bash isn't enough; the shell used when you open a new Terminal window is still Zsh. Let's go through the steps to change this to the new Bash we just installed.
2-1. Add the New Bash to the List of Allowed Shells
For security reasons, macOS maintains a list of programs that users are allowed to set as their default shell in a file at /etc/shells. First, we need to add the path to the Homebrew-installed Bash to this file.
Please run the following command. When prompted for a password, enter the login password for your Mac.
sudo sh -c 'echo /opt/homebrew/bin/bash >> /etc/shells'
2-2. Change the Default Shell with the chsh Command
Once it's added to the allowed list, use the chsh (change shell) command to change your login shell to the new Bash.
chsh -s /opt/homebrew/bin/bash
After running this command, you must completely quit and reopen the Terminal. From then on, the Terminal will start with Bash.
Step 3: Verification and How to Switch Back
Once you open a new terminal window, let's check if the shell has really changed to Bash. Run the following command.
$ echo $SHELL
/opt/homebrew/bin/bash
If the path to the Bash installed by Homebrew is displayed, the switch was a success!
When You Want to Switch Back to Zsh
If you start missing Zsh's features or just want to switch back, it's easy. Just use the chsh command again, but this time specify the path to Zsh.
chsh -s /bin/zsh
After this, open a new terminal window, and you'll be back to the familiar Zsh prompt.
Conclusion
Great work! In this article for macOS users, we explained how to switch the default shell from the standard Zsh to the latest version of Bash. Now your Mac is nearly identical to the Bash environment widely used in web development, which should make your learning and work much smoother.
Both Zsh and Bash are excellent tools, each with its own pros and cons. Neither one is absolutely better than the other. What's important is to understand the features of each and to have the knowledge to switch your environment at any time to suit the requirements of a project or your personal preference. With today's article, you have gained that power!
Now that your Bash environment is ready, the next step is to learn the basic Bash commands one by one. In the next article, we'll go into detail on file operation basics like `cd` and `ls`. This is the fundamental strength training for using Bash, so we hope you'll continue with us.
A Guide to Basic Bash Commands and How to Use Them (cd, ls, echo, etc.)