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

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

by Travis Rose
Published: Updated:
zsh command

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.

 

You may also like

4 comments

Roman Perepelitsa November 17, 2019 - 10:42 AM

There is powerleve10k now. Over 10 times faster than powerlevel9k and all around better.

Reply
Travis Rose November 21, 2019 - 12:10 PM

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!

Reply
Roman Perepelitsa December 28, 2019 - 5:06 AM

Did you get a chance to try it?

Reply
Travis Rose December 31, 2019 - 11:45 AM

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!

Travis

Reply

Leave a Comment

fl_logo_v3_footer

ENHANCE YOUR LINUX EXPERIENCE.



FOSS Linux is a leading resource for Linux enthusiasts and professionals alike. With a focus on providing the best Linux tutorials, open-source apps, news, and reviews written by team of expert authors. FOSS Linux is the go-to source for all things Linux.

Whether you’re a beginner or an experienced user, FOSS Linux has something for everyone.

Follow Us

Subscribe

©2016-2023 FOSS LINUX

A PART OF VIBRANT LEAF MEDIA COMPANY.

ALL RIGHTS RESERVED.

“Linux” is the registered trademark by Linus Torvalds in the U.S. and other countries.