Talk Tech to Me: 5 Z Shell Features That Will Make You Want to Switch from Bash

The Bash shell is the most common login shell used on Linux systems. So, why use the Z shell? Simply put, because it can do more than Bash.
Talk Tech to Me, brought to you by CompTIA

The command line interface on your system is provided by a shell. And while there are dozens of different shells available to choose from, nearly every Linux system over the past two decades has used the feature-rich Bash shell as the default login shell. As a result, the Bash shell is what you need to know in order to pass industry Linux certifications, like CompTIA Linux+.

Although the Bash shell still remains the most common login shell used on Linux systems, macOS and a few Linux distributions have switched their default shell to the Z shell (zsh), including the Kali Linux distribution that is commonly used by cybersecurity professionals. The Z shell has been around for three decades and is the second most popular shell next to Bash.

Get to Know the Z Shell (zsh)

On the surface, the Z shell is nearly identical to the Bash shell; it has the same command line and shell scripting features, but displays a %_ prompt instead of a $_ prompt.

As a Bash shell user, this means that you can immediately start working within a Z shell, as well as create shell scripts exactly like you would in a Bash shell (the only change would be using #!/bin/zsh instead of #!/bin/bash on the first line of the shell script).

The Z shell also has a similar environment file structure. The ~/.zshrc file works just like ~/.bashrc in that it is executed for each new Z shell you create. And just like /etc/bashrc, there’s a /etc/zshrc that contains system-wide Z shell options. Similarly, the /etc/bash_profile and ~/.bash_profile login scripts also have Z shell equivalents (/etc/zshprofile and ~/.zshprofile).

So, if the Z shell is so similar to Bash, why do some prefer using it? Simply put, because the Z shell can do more than Bash.

What Can Z Shell Do? 5 Awesome Features

Let’s examine five awesome features that you can try in your own Z shell throughout this blog post. Even if it isn’t your default login shell, the Z shell is typically installed by default on most Linux systems. Thus, you can obtain a Z shell by typing zsh within an existing Bash shell.

If you decide to use the Z shell as your login shell instead of Bash after reading this article, simply run the chsh -s /bin/zsh command.

Let’s get started.

1. Z Shell Flexible Redirection and Piping

Using Z shell, you can redirect standard input, output and error to multiple files, as well as use standard output redirection within a pipe (eliminating the need for the tee command).

For example, the following command sends the standard output of the lsblk command to both file1.txt and file2.txt:

% lsblk >file1.txt >file2.txt

You could instead send the standard output to file1.txt as well as pipe it to the grep command to extract “xfs” lines and save them to file2.txt:

% lsblk >file1.txt | grep xfs >file2.txt

Redirecting standard input from multiple files is a Z shell feature I use often. The following will merge and sort bigfile1.txt and bigfile2.txt, saving the combined sorted output to output.txt:

% sort <bigfile1.txt <bigfile2.txt >output.txt

Another neat Z shell trick is to use input redirection to view files. The following is an alternative to running the more output.txt command:

% <output.txt

2. Z Shell Options

By setting options, you can control nearly every aspect of the Z shell, including auto-completion, file globbing, command history, input/output processing, shell emulation, function handling and more. Here’s a full list of Z shell options.

Two options that I regularly use are autocd and correct. The easiest way to set these two options is to add the line setopt autocd correct to your ~/.zshrc file. After doing this, you’ll need to start a new Z shell (or run source ~/.zshrc to reload ~/.zshrc in your current shell).

The autocd option allows you to change to a directory by specifying the directory name only (without the cd command).

For example:

% pwd

/home/jaoneckert

% Desktop

% pwd

/home/jasoneckert/Desktop

The correct option allows your shell to auto-correct misspelled commands. For example, if you type the cal (calendar) command incorrectly:

% cla

zsh: correct 'cla' to 'cal' [nyae]? y

   November 2020     

Su Mo Tu We Th Fr Sa 

 1  2  3  4  5  6  7 

 8  9 10 11 12 13 14 

15 16 17 18 19 20 21 

22 23 24 25 26 27 28 

29 30                      

3. Z Shell Modules

The built-in commands in the Z shell are modular; you can choose which ones are available by loading the appropriate modules. Here’s a full list of available Z shell modules.

To view the default modules loaded in your Z shell, run the zmodload command without arguments:

% zmodload

zsh/complete

zsh/main

zsh/parameter

zsh/regex

zsh/terminfo

zsh/zle

zsh/zutil

To get additional built-in commands in your Z shell, you can choose to load additional modules. For example, to use the strftime built-in command to obtain detailed time and date information, you must load the zsh/datetime module, as shown below:

% zmodload zsh/datetime

% strftime "%a, %d %b %y %T %z"

Sun, 01 Nov 20 08:57:08 -0500

%

To ensure that the datetime module is loaded each time you start a Z shell, add the line zmodload zsh/datetime to your ~/.zshrc file.

Using built-in commands in Z shell scripts makes them more robust and portable. While the date command on one system may not support the same options as on another system, the strftime built-in Z shell command will always work the same way on any system running the Z shell.

Similarly, by loading the zsh/mathfunc module, you can use mathematical functions such as sqrt (square root) within a Z shell script without having to rely on other programs installed on the underlying Linux system.

4. Z Shell Functions

The Z shell also supports the loading of additional functions from directories specified in the $fpath variable. These directories contain many useful functions by default, and programs you install may add even more.

One of my favorite functions is compinit, which extends the built-in Tab key completion feature for commands and filenames to include command options and arguments as well.

To ensure that this function is loaded each time you start a new Z shell, add the line autoload -U compinit && compinit to your ~/.zshrc file.

Next, when you type a command like tar, you can type - and press the Tab key to see the available options:

% tar -

A  -- append to an archive

c  -- create a new archive

f  -- specify archive file or device

t  -- list archive contents

u  -- update archive

v  -- verbose output

x  -- extract files from an archive

If the available options are POSIX options (e.g. --universal), you can also use the Tab key to autocomplete the option name after typing the first few letters.

For complex commands that require specific option combinations, you’ll be prompted to select required options before selecting secondary options. Additionally, for commands that accept a predefined list of arguments, you can use the Tab key to autocomplete those arguments as well.

5. Z Shell Themes and Plugins

Perhaps the most attractive feature of the Z shell is its support for plugins and themes. There are currently over 250 plugins that provide a wide range of additional functionality, from integrating git features into the Z shell prompt to displaying the current weather in your terminal using Unicode symbols. If you’d like to modify the look and feel of your Z shell, there are over 150 themes available for you to add. Search for and install Z shell plugins and themes.

As with other Z shell configurations, you specify the theme and plugins you wish to use in your ~/.zshrc file. For example, the following lines apply the agnoster theme as well as load the git and dotenv plugins:

ZSH_THEME="agnoster"

plugins=(

  git

  dotenv

)

While the Bash shell still remains the most common login shell used on Linux systems, the Z shell has some major advantages. Try out these features in your own Z shell and let us know what you think.

CompTIA Linux+ validates the skills employers need for Linux administrator jobs. Download the exam objectives to see what other Linux skills they look for.

Jason W. Eckert has been a UNIX (and later Linux) user/developer/sysadmin for more than 30 years, He has taught UNIX and Linux topics, including the vi text editor, in the college space for more than 20 years.

Email us at [email protected] for inquiries related to contributed articles, link building and other web content needs.

Read More from the CompTIA Blog

Leave a Comment