What is Zsh? Explaining Why Developers Love It and Its Differences from Bash
In the series so far, we've learned about the king of shells, "Bash," and the ace up Windows' sleeve, "PowerShell." I hope you've gotten a sense of how powerful the command-line world is and its potential to dramatically improve your workflow. Now, to wrap it all up, we'll introduce the ultimate shell, "Zsh" (Z Shell), which many developers rave about, saying, "once you use it, you can't go back."
Especially on macOS, Zsh has been adopted as the default shell for several years now, making it an unavoidable presence for web creators. We'll answer questions like "What's the difference from Bash?" and "Why is it so popular?" in an easy-to-understand way, with samples that let you experience its specific features and feel.
If Bash is a reliable domestic sedan, then Zsh is like a luxury sports car packed with convenient features and endless customization. By the time you finish this article, you're sure to see the appeal of Zsh and will actually look forward to opening your terminal!
What is Zsh (Z Shell)?
Zsh, like Bash, is a type of Unix shell. It was developed with the goal of incorporating the good parts of Bash while adding and expanding upon them with more powerful features. For this reason, basic command operations (like ls, cd, rm, etc.) and the use of pipelines (|) are almost identical to Bash. You can think of it as a kind of superset of Bash where your Bash knowledge is directly applicable.
So, what's the reason for Zsh's immense popularity among developers? The secret lies mainly in the following three features:
- Powerful completion features: Drastically reduces input errors and improves speed.
- Thoughtful convenience functions: Packed with features that scratch that itch, like shortcuts for directory navigation.
- Rich customizability: Using frameworks like "Oh My Zsh," you can infinitely extend its appearance and functionality to your liking.
Rather than explaining with words, the best way is to experience the difference for yourself. In the next section, let's take a look at some of Zsh's amazing features.
Experience the Charm of Zsh! Convenient Features That Show the Difference from Bash
Here, we'll introduce a few of Zsh's most convenient and easy-to-experience features. Please try them out in your terminal (on macOS or a Linux environment with Zsh installed).
1. The "Too Smart" Completion Feature
Typing part of a command or filename and pressing the Tab key to complete the rest is a feature that Bash also has. However, Zsh's completion feature goes far beyond that.
For example, what if you forgot the options for the git command? Try typing git checkout - and then press the Tab key.
$ git checkout -[Press Tab Key]
A list of available options will appear with descriptions, and you can select one with the arrow keys. You don't need to look up the manual anymore.
Directory navigation also gets smarter. For example, when you want to move to a directory deep in the hierarchy, you can just type the first letter of each level to complete it.
$ cd /u/l/b[Press Tab Key]
# It will automatically be completed as follows:
$ cd /usr/local/bin/
2. Directory Navigation without the `cd` Command
In Zsh, if you enable an option setting (setopt AUTO_CD), you can move to a directory just by typing its name, as if you had typed the cd command.
$ ../../
# Moves up two directory levels
3. Advanced File Searching (Globbing)
Globbing refers to filename expansion using wildcards (like *). In Zsh, this is extremely powerful.
For example, if you want to find all files with a .html extension in all subdirectories under the current directory, you can write it like this:
$ ls **/*.html
The ** signifies a recursive search. Doing this in Bash requires additional settings and is a bit more cumbersome.
"Oh My Zsh" - The Framework That Makes Zsh a Beast
What unleashes the full potential of Zsh are "frameworks" like Oh My Zsh. This is a system that makes it easy to manage useful Zsh settings, plugins, and themes.
By installing the particularly famous Oh My Zsh, you can immediately enjoy benefits like these:
- Convenient aliases: For example, common Git operations like
git statuscan be run withgst, andgit pushwithgp. - Enhanced Git integration: The current Git branch name and modification status are always displayed in the prompt, helping to prevent work mistakes.
- A wealth of themes: You can change the look of your prompt to qualquer one of hundreds of designs in an instant.
Trying to do all these settings yourself would be a huge task, but with Oh My Zsh, you can make the useful settings created by developers around the world your own in just a few minutes.
Compatibility with Bash and Points to Note When Writing Scripts
As we've seen, for interactive use, Zsh can be used as almost a complete superset of Bash. However, when writing shell scripts, there is one important difference to be aware of.
That is the starting number of array indices.
In Bash (indices start from 0):
#!/bin/bash
my_array=("apple" "banana" "cherry")
echo ${my_array[0]} # -> apple
In Zsh (indices start from 1):
#!/bin/zsh
my_array=("apple" "banana" "cherry")
echo $my_array[1] # -> apple
This difference can cause unexpected errors, for example, when running a script written for Bash in Zsh. It is important to correctly write the shebang (e.g., #!/bin/bash) on the first line of the script to make it clear which shell it is intended to be run with.
Conclusion
This time, we've explained the appeal of the "Zsh" shell, which has become the standard on macOS and is beloved by many developers.
- You can leverage your Bash knowledge while using even more convenient features.
- Powerful completion reduces typing and prevents mistakes.
- With frameworks like Oh My Zsh, you can freely customize both appearance and functionality.
Zsh is a very powerful tool that can transform daily command-line work from a "tedious task" into a "comfortable and creative experience." As a web creator, now that tools like server operations and Git are unavoidable, mastering Zsh will be a huge advantage.
For those of you who are now thinking, "I definitely want to install Zsh on my own machine!", the next article will explain the specific steps to install Zsh and Oh My Zsh on macOS and Linux and perform the initial setup. Stay tuned!
How to Install Zsh (macOS / Linux) and an Initial Setup Guide