Chances are, as a Linux user, when you enter a command in the CLI, you’re using the bash shell. While bash, short for Bourne-again shell, is the default shell for most Linux distros, it is not the only shell “game” in town. There are many more.
In Linux or Unix, the shell is just the operating system’s command interpreter that allows the user to execute other programs.
Some standard Linux shells include:
- ksh (Korne shell)
- csh (c shell)
- dash (Debian Almquist shell)
- tcsh (TENEX c shell)
- fish (Friendly interactive shell), and
- zsh (z shell)
In today’s article, we’ll take an in-depth look at the zsh shell, as it is growing increasingly popular with the *Nix community. Earlier this year, Apple announced that starting with the macOS 10.15 Catalina, zsh should serve as the OS’s default shell, replacing bash, used by Apple since the macOS X 10.3 Panther, first released in October 2003.
Our Linux distro for our zsh exploration is Ubuntu 19.10. As the zsh bash is not installed by default on Ubuntu, let’s install it.
# sudo apt install zsh
Let’s take a quick look at what the zsh shell.
Running the zsh command for the first time invokes the Z shell configuration function and offers us several options:
- (q) Quit and do nothing. The function will be rerun next time.
- (0) Exit, creating the file ~/.zshrc containing just a comment. That will prevent this function from being run again.
- (1) Continue to the main menu.
- (2) Populate your ~/.zshrc with the configuration recommended by the system administrator and exit (you will need to edit the file by hand if so desired).
If we type q, nothing changes, and the next time we type zsh, the same screen should greet us.
Typing 0 creates a .zshrc file in our home directory (~/). However, this file contains only a comment file in the file and runs no commands.
If we type 1, we’ve presented the main menu, which allows us to populate our ~/.zshrc file based on the answers we provide.
Lastly, if we type 2, our ~/.zshrc file is populated with the default settings.
Press 2. Don’t worry; we’ll get back to the bash shell at the end of this article).
When you’re returned to the prompt, you’ll notice that your CLI prompt looks different. This is because your shell is no longer the bash shell, you’re now using the zsh shell (within the bash shell).
Let’s make sure.
# echo $0
You can also exit back to the bash shell with the exit command.
The file that option 2 created for us is the ~/.zshrc file and can be edited by you at any time.
You’ll notice if you look at the hidden files in your home directory, you know, have a ~/.zschrc file along with your ~/.bashrc file. Each shell has its own “rc” file. The “rc” file is simply the configuration file for your shell. Common uses for “rc” files include setting custom environmental variables and setting an alias for commonly used commands.
alias update="sudo apt-get update && sudo apt-get upgrade"
Note, however, although we did install the zsh shell on our system, it is not our default shell. The bash shell is still the default shell. We can easily change the default shell to zsh.
First, however, let us see what shell options are by taking a look at /etc/shell.
# cat /etc/shells
Now that we see zsh is there and we know the directory, let’s set our default shell to zsh.
# chsh -s /bin/zsh
Notice that the prompt didn’t change this time. That’s because the change will not take effect until we reboot the system.
Just as quickly, we can change it back to the bash shell.
# chsh -s /bin/bash
As before, however, we must reboot before our change takes effect.
So, great, you now know that there are other shells available to you, as well as how to install and implement them on your system. But what does the zsh shell offer you that the bash shell does not?
While the features that ZSH offers are far too numerous to list here, let’s take a look at some of the major ones.
Change Directory Tab Completion
When you use the change directory (cd <Tab>) command in bash, you’re prompted with a list of files in the current directory.
# cd <Tab>
A list of files presented as a result of the cd <Tab> command is not particularly useful. The zsh shell recognizes this and provides you a list of only valid directories.
Shorthand List Command
When manually navigating directories in the CLI, having to remember a string of text such as /home/mtravisrose/Desktop can be daunting. Luckily, with zsh, you don’t need to remember the entire directory name, just the first few letters (enough to make it unique), in this case,
# cd /h/m/De
and zsh will figure out the rest.
Tab Completion with the kill Command
While most bash users only kill processes running after inspecting them using the ps command (or the pkill command for those who live dangerously), zsh allows you to type the kill command along with the first letter or the process you want to kill. Press the <Tab> and presents you a navigable keyboard list of command that starts with the letter(s) you typed after the kill command.
Other cool features of zsh include shared command history, environmental variable expansion, built-in globbing support, automatic change directory, spelling correction, and command auto-correction. Look, it’s impossible to list, demonstrate the plethora of attractive features zsh adds to your CLI. Many of the features are deserving of an article of their own. But perhaps the most awesome, and unquestionably most well-liked feature of the zsh shell is its support of plugins and themes.
A zsh plugin is just a framework for managing your zsh configuration, while the theme is merely the aesthetics.
By far, the most popular plugin framework for zsh is Oh-My-Zsh. Oh-My-Zsh comes replete with over 250 built-in plugins and 125 themes as well.
As for zsh themes, powerlevel9k is far and above the most popular and gets much copy and mentions on the internet. As the title of one article declares, “The coolest Linux Shell EVER!”.
Whether new Linux users or a seasoned professional, we encourage you to install the zsh shell and run it through its paces. Let us know your thoughts and experiences. The zsh man page is an excellent resource for finding out more about the zsh shell, including such topics as startup files, filename generation, login/logout watching, and indexes such as the key index, concept index, and functions index.
# man zsh
If you like the zsh shell and would like us to pen further zsh-related articles such as an overview of Oh-My-Zsh and the powerlevel9k theme, please let us know, and we’ll oblige.
We look forward to hearing from you.
There is powerleve10k now. Over 10 times faster than powerlevel9k and all around better.
Wow! Thanks for the heads up Roman. I’m looking forward to taking it for a spin this weekend and kicking the tires. Good looking out!
Did you get a chance to try it?
Sorry, Roman, I just saw this. I’ve been inundated as of late.
Also, Happy New Years!
While I’ve begun the installation and configuration of everything, I have a ways to go yet. I’m actually basing a lot of my configuration off of what I’ve gleaned from Deepu Sasidharan’s page (deepu.tech/configure-a-beautiful-terminal-on-unix/).
I really like what I see so far, and am hoping the days up until next Monday afford me to delve into it more. I’d like to see if I’m able to go ahead and use what I learn into a combination Oh-My-Zsh and powerlevel10k article shortly thereafter (the first week of the new year).
I’ll keep you posted. Thanks for checking on me!