Taking command-line power user’s ZSH for a spin – no shell game here

ZSH is available in the repositories of almost all Linux distributions. It has all the features of BASH, and something more that you love!

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

Install the zsh shell on Ubuntu 19.10 via "sudo apt install"
zsh shell installation

Let’s take a quick look at what the zsh shell.

#zsh

Initial screen when running zsh.
Initial screen when running zsh

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.

zsh Main Menu
zsh Main Menu

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).

Option 2 at the zhs main menu creates and populates the ~/.zshrc file.
zhs main menu creates and populates the ~/.zshrc file

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.

# exit

Easily switch to, verfiy, and exit the zsh shell from within bash.
Exit the zsh shell from within bash

The file that option 2 created for us is the ~/.zshrc file and can be edited by you at any time.

A generic, populated ~/.zshrc file.
A generic, populated ~/.zshrc file

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.

For example:

export_EDITOR=gedit

or

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.

Set your default shell with the chsh command. Don't forget to reboot.
Set your default shell with the chsh

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.

Typing cd /h/m/De auto corrects to cd /home/mtravisrose/Desktop.
Typing cd /h/m/De auto corrects to cd /home/mtravisrose/Desktop

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.

The Oh-My-Zsh Plugin.
The Oh-My-Zsh Plugin

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!”.

The powerlevel9k zsh theme in action.
The powerlevel9k zsh theme in action

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

The zsh man page is a great resource for finding out more about the zsh shell.
The zsh man page

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.

 

Travis Rose
Hi, I'm M Travis Rose, a contributor to FOSS Linux. I have over thirty years of experience in the IT arena, at least fifteen of which has been working with Linux. I enjoy converting existing Windows users to the wonderful world of Linux. I guess you could call me a Linux-evangelist. Long live Linux!

2 COMMENTS

    • 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!

LEAVE A REPLY

Please enter your comment!
Please enter your name here

STAY CONNECTED

24,270FansLike
192FollowersFollow

LATEST ARTICLES

gamemode enable linux
GameMode is a combination of various libraries and daemons that allows all the users to improve the gaming performance on the Linux system. Developed by games publisher Feral Interactive, it improves gaming performance by requesting a group of options that will be applied temporarily to the Linux system.
best linux distros for laptops
Whether buying a Linux pre-installed laptop or selecting a Linux distro for your existing laptop, there are many things to consider. Let's take you through some of the best Linux distros that are optimized for Laptops in this 2019 edition of the article. Read on.
Ubuntu 19.10 beta overview
It may be early, but I've been looking forward to the release of Ubuntu 19.10 for some time now. As an impatient person, and promised readers in the FOSSLinux article announcing the release of Eoan Ermine that we would provide you a review of Ubuntu 19.10 Eoan Ermine - Beta.  So, here it goes.