5 Z shell features that will encourage you to switch from Bash

+

tux zsh shell

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 UNIX and Linux system over the past two decades has used the feature-rich Bash shell as the default login shell.

Although the Bash shell still remains the most common login shell used on UNIX and Linux systems, macOS and a few Linux distributions (e.g. Kali) have switched their default shell to the Z shell (zsh), and this trend is likely to continue. The Z shell has been around for three decades, and is the second most popular shell next to Bash.

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 Z shell is so similar to Bash, why do some users prefer using it? The simple answer is: Z shell can do more than Bash.

We’ll examine five awesome Z shell features that you can try throughout this blog post. Even if it isn’t your default login shell, the Z shell is installed by default on most UNIX and 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 blog post, simply run the chsh -s /bin/zsh command.

1. Flexible redirection and piping

Using the 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 Z shell options, you can control nearly every aspect of the shell, including auto-completion, file globbing, command history, input/output processing, shell emulation, function handling, and more.

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            
%              

For a full list of options available, visit http://zsh.sourceforge.net/Doc/Release/Options.html

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. 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 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 UNIX or Linux system. For a full list of modules available, visit http://zsh.sourceforge.net/Doc/Release/Zsh-Modules.html

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

To search for and install Z shell plugins and themes, follow the instructions at https://github.com/ohmyzsh/ohmyzsh

As with other Z shell configuration, 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
)