- History of Zsh Shell with macOS
- Why Bash was replaced by Zsh Shell in macOS?
- What is Zsh?
- History of Zsh
- Zsh Startup Files
- How to defile aliases in Zsh Shell?
- How to define Functions in Zsh Shell?
History of Zsh Shell with macOS
Apple released macOS Catalina at WWDC 2019 on October 7, 2019, it was shipped with Zsh shell as the default login shell and interactive shell, replacing the Bash shell which had been the default shell since the Mac OS X Panther that was released in the year 2003.
Though Bash is still shipped with all the macOS ever since Catalina till date (macOS Ventura 13 being the latest release) when you open the Terminal app you will see that the prompt is a Percentage (% = Zsh) and not Hash (# = Bash)
If you move from Zsh shell to bash you will see a warning "The default interactive shell is now zsh"
% bash
The default interactive shell is now zsh.
To update your account to use zsh, please run `chsh -s /bin/zsh`.
For more details, please visit https://support.apple.com/kb/HT208050.
bash-3.2$
Why Bash was replaced by Zsh Shell in macOS?
To know why, lets go to the GNU page for Bash and look for its releases: https://ftp.gnu.org/gnu/bash/
Bash Version | Release Date |
---|---|
bash-1.14.7 | 1996-08-29 |
bash-2.0 | 1996-12-31 |
Bash 3.0 | 2004-08-03 |
bash-3.2 | 2006-10-11 |
bash-4.0 | 2009-02-20 |
bash-5.0 | 2019-01-07 |
bash-5.1 | 2020-12-07 |
Now let me see the version of Bash that's shipped with macOS Monterey 12.2,
% sw_vers
ProductName: macOS
ProductVersion: 12.2.1
BuildVersion: 21D62
% bash -version
GNU bash, version 3.2.57(1)-release (arm64-apple-darwin21)
Copyright (C) 2007 Free Software Foundation, Inc.
![Bash Version on macOS is old 3.2.57 Bash Version on macOS is old 3.2.57](https://code2care.org/zsh/zsh-shell-mac-tutorial/images/Bash Version on macOS is old 3.2.57.png)
As you can see that the version for Bash is quite old 3.2 that was released back in the year 2006!, while we have version 4 that was in year 2009 and version 5 in the year 2019.
Why? Though Apple have not provided the reason for it, it is clear if you see the license of Bash v3.2 it is GPL v2 (General Public Licence) where as all Bash versions released after it are GPL v3.
- GNU Free Documentation License Version 2, November 2002
- GNU Free Documentation License Version 3, 29 June 2007
Zsh shell had been getting popular among users and its licensed under MIT-Modern-Varian
What is Zsh?
-
Zsh, read as Z shell is a Unix Shell (Bash is a Unix Shell as well)
Zsh shell is shipped as the default login and interactive shell with Apple macOS since Catalina
It is an extension of sh (Bourne shell)
Zsh is incorporated with most of the features from bash, zsh, tcsh and sh shell along with many new features.
Zsh is also a powerful scripting language.
History of Zsh?
The first version of Zsh was written by Paul Falstad in the 1990
The name zsh has been derived from the name of professor Zhong Shao of Yale University.
Zsh Startup Files
Just like bash, Zsh shell has a list of startup files that helps to create the environment you interact with.
~/.zshenv : Zsh Environment File
The .zshenv file is always sourced, so it is the ideal file to place all your environment variables. The most commonly used $PATH, $JAVA_HOME, $ZDOTDIR environment variables can go in here. All variables that are shared across programs can go in here.
Example:
Adding PATH and other environment variables:% nano ~/.zshenv
Apply changes:PATH=$PATH:/opt/homebrew/bin PROD_ENV_DIR=/usr/bin/mydir
Testing:% source ~/.zshenv
% echo $PROD_ENV_DIR /usr/bin/mydir
~/.zprofile : Zsh Profile File
.zshprofile file is kind of similar to .zlogin, except that it is source before the .zshrc, you can also use this file as an alternative to .zlogin
Note: It is recommended that you do not use both .zprofile and .zlogin files together!
~/.zshrc : Zsh Interactive shell configuration file
.zshrc is the one that is associated with the interactive shell. setopt and unsetopt commands can be used here. The variables that not to shared across programs can be set here. You can also place aliases here.
~/.zlogin : Zsh login File
As discussed earlier, .zlogin file is similar to .zshenv, this file is read before .zshrc
~/.zlogout: Zsh logout File
As you must have realized from its name, this file is read at the time of exiting the zsh shell. It will clear and reset the terminal.
Last login: Sat Mar 12 21:36:40 on ttys000
.zshenv Startup file read...
.zshprofile Startup file is raed..
.zshrc Startup file read...
.zlogin Startup file read...
code2care@Code2cares-MacBook-Air ~ %
How to defile aliases in Zsh Shell?
There are many commands that we use in the Terminal often that may have many parameters (optional parameters) or are hard to remember and may take a while to type, alias command can help you to create shortcuts or name such commands with aliases that can help you speed up.
Example:desktop: Most often I save files on my Desktop, so whatever directory I am in and I want to go to Desktop this alias takes me there,
Open ~/.zshrc file and add the below line,
alias desktop='cd /Users/code2care/Desktop'
Apply the changes "source ~/.zshrc", now if you desktop will run as a command,
% desktop
% pwd
/Users/code2care/Desktop
Let's see one more example, if you are used to using cls command on Windows Command Line, you might find it useful to set it as an alias to so you can use either of cls or clear.
alias cls="clear"
Read More: https://code2care.org/zsh/zsh-shell-custom-alias-useful-examples
How to define Functions in Zsh Shell?
You can create your own custom functions in Zsh Shell, lets start with "Hello World" example,
Open .zshrc file and add the below function,
function myZshHelloWorldFunction () {
echo "Hello World!"
}
Apply changes source ~/.zshrc, now if you type myZshHelloWorldFunction on the prompt you will see "Hello World!" returned on the prompt,
% myZshHelloWorldFunction
Hello World!
Facing issues? Have Questions? Post them here! I am happy to answer!
Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.
Follow him on: X
You can also reach out to him via e-mail: rakesh@code2care.org
- Fix: grunt: command not found (Mac/Windows)
- AppleScript Example with TextEdit
- How to copy Password of Wifi on your Mac
- Install OpenJDK Java (8/11/17) versions using brew on Mac (Intel/M1/M2)
- How to check if a port is in use using terminal [Linux or macOS]
- Cut, Copy and Paste Keyboard Shortcuts on Mac Keyboard
- [macOS] NetBeans IDE cannot be installed. Java (JRE) found on your computer but JDK XX or newer is required.
- How to Remove Widgets from macOS Sonoma 14 Desktop
- Turn off Auto-Capitalization on macOS Ventura 13
- How to Go To /usr/local/bin on Mac Terminal?
- How to Open TextEdit on Mac
- Mac: How to Open Android Studio from Terminal
- How to change the default location of Mac Screenshot
- Display Seconds in Digital Clock on Mac Menu Bar [macOS]
- How to install homebrew (brew) on M1 Mac
- Must Know Homebrew Commands for Mac/Linux Users
- Steps to Uninstall Roblox on Mac (macOS)
- How to change the System Settings Sidebar icon size Mac Ventura 13
- How to set Out Of Office (OOO) on Outlook for Mac [Updated 2023]
- Trigger Notification Center Message using Mac Terminal Command
- [fix] macOS Ventura - Python3 xcrun: error: invalid active developer path missing xcrun at CommandLineTools
- How to Install Rust Language on Mac (macOS)
- How to minimize all Windows in Mac
- Display ls command file sizes in KB (kilobytes) MB (megabytes) or GB (gigabytes) [Linux/macOS]
- How to make TextEdit the default text Editor on Mac
- How to know file encoding in Microsoft Windows Notepad? - Microsoft
- Submit html form on dropdown menu value selection or change using javascript - JavaScript
- [Android] This view is not constrained vertically: at runtime it will jump to the top unless you add a vertical constraint - Android
- How to adjust MacBook Desktop icons size - MacOS
- How to check if a variable is set in Bash Script or Not - Bash
- How to enable Do Not Disturb (DND) mode in Microsoft Teams - Teams
- Install Bash Completion on macOS - Bash
- Program 10: Modulo of Two Numbers - 1000+ Python Programs - Python-Programs