A shell is used as an interface between a user and the operating system. On most Linux distributions, bash shell is the default shell used, however, the best part about open source is choice. As users, we can decide to change defaults that came with the system and use other alternatives.
In Linux, the most commonly used open-source shells include Bourne Again Shell (bash), tcsh (an enhanced C shell), Korn shell (ksh), friendly interactive shell (fish), and zsh.
Zsh is an extended version of the bash shell with added features and it is based on a similar shell as bash therefore bash commands are also applicable in zsh.
In this article, we discuss how to install and use zsh shell features in Linux systems.
Installing Z Shell (Zsh) in Linux
Depending on the Linux distribution you are using, you can install Zsh on your Linux system from one of the following commands:
$ sudo apt install zsh [On Debian, Ubuntu and Mint] $ sudo yum install zsh [On RHEL/CentOS/Fedora and Rocky Linux/AlmaLinux] $ sudo emerge -a app-shells/zsh [On Gentoo Linux] $ sudo pacman -S zsh [On Arch Linux] $ sudo zypper install zsh [On OpenSUSE]
To get started, open a terminal and type the command to change the current shell to zsh:
We can also use the chsh command to change the default shell although this requires us to log out then log back in for the changes to take effect.
$ chsh -s $(which zsh)
How to Use Z Shell (Zsh) in Linux
At this point, if the shell has changed, we can start exploring some of the zsh features:
1. Tab Completion
As an example, type the cd command then, press the tab key:
$ cd <TAB>
Unlike bash shell, which lists all files and directories, zsh, in addition to listing all files and directories, allows us to navigate directories while highlighting them. When we find a directory to change to, we just press the enter key.
2. History Command
With bash, we usually use the history command or perform a recursive search (CTRL+R) to find the last executed commands.
Zsh on the other hand gives better search functionality whereby we just type in the command then use the arrow up button to find the last commands executed with the command.
For example, to find all last executed cd commands we type cd and cycle through them using the arrow up button:
$ cd <ARROW UP>
3. Killing Linux Processes
Usually, we use the htop or ps command to find and kill unwanted processes. Zsh performs this action even better.
To demonstrate, type in the kill command and press the tab key:
$ kill <TAB>
To kill, say, the bash process with PID 38787, we press the enter key while we have it highlighted.
4. Command Options
In bash, say we forgot what ping command option allows us to send n number of packets, first we would have to abort writing the current command to type in
ping --help or read the command’s manual page.
$ man ping
With Zsh all we have to do is write the command followed by a hyphen:
$ ping -
From the output, we can see some ping command options. For more command options, we press the tab key.
$ ping - <TAB>
To move down or up the options list we use the up and down arrow keys or the tab button. Also, notice how they are auto-filled to the ping command prompt, all we have to do to proceed is to pass a number representing the number of packets (5) we wish to send along with an IP address (184.108.40.206).
$ ping -c 5 220.127.116.11
Zsh shell can correct us when we make a mistake or help us remember what the exact name of a file or directory was.
For example, assume we forgot a directory name but remember some characters in the middle of the file name, or we remember the file extension.
In this case, assuming we forgot the Templates directory. We can write:
$ ls lates
Immediately you will see the Templates directory listed, now all we have to do is press the tab key to autofill and correct our mistake.
$ ls <TAB>
We can also apply the same concept to search for files with specific file extensions, for example, if we are searching for
.c files we write.
$ cat .c
Now, all we have to do is press the tab key and highlight files then when the correct file is highlighted we select it by pressing the enter key.
Zsh is designed as an interactive shell and also a powerful scripting language. It is a combination of bash, ksh, and tsh shells all in one powerful shell.
We have discussed just a few features, enough to motivate one to make the shift from bash to zsh. In addition to more great features, it also has plugin support and helpful documentation that will blow your mind. And, there is no learning curve since bash commands are also applicable here.