Understanding The Different Shell In Linux : Zsh Shell

Spread the love

After Bash and Korn, today we shall discover the very popular Zsh shell. In fact, Zsh is so popular that a lot of people has left Bash for him. The main reason behind its popularity is due to an incredible interactivity with the user, and a very comprehensive and powerful auto-completion functionality.

History

The first version of Zsh was created in 1990 by a Princeton Student named Paul Falstad. The name “Zsh” supposedly comes from the connection ID of the Princeton professor Zhong Shao. The shell in itself was influenced from Bash, Ksh, and Tcsh: Zsh has a very powerful auto-completion functionality, along with recursive searches and a corrector included. And of course, the prompt can be defined according to the user’s wish. For all that, some consider Zsh as an extended Bourne Shell.

Installation

Now that you know more about Zsh, let’s install it. For Ubuntu, the installation is fairly simple:

sudo apt-get install zsh

For other distributions, it should be available in your repositories too. In the worst case, download the package from the official page.

Once the package is installed, you can launch a Zsh Shell from any terminal via the command:

zsh

If you really like this shell and want it to replace your current one, you can do that by typing

sudo usermod -s /bin/zsh [user name]

Notice that as usual, the configuration file is at “~/.zshrc” and will be created on the first launch. As the same as bash, this can be used to add aliases and personalize the prompt.

Usage

Zsh is very intuitive. At first, you should not notice any difference in behavior with Bash. However, a good way to spot the first feature is to type:

rm - [tabulation key]

and then use the tabulation key. This will display all the possible arguments for the “rm” command. As you can see, the auto-completion is incredible with Zsh.

Not only a command’s arguments, but also the ssh hosts presents in /etc/hosts, the possible targets for the “make” command, the manual pages, the files on a distant server, etc, all can be auto-completed by Zsh. And the auto-completion speed is also impressive.

You can also try type a wrong command like

gdit .zshrc

and it will trigger the correction feature:

If the correction does not work at first, it means that you haven’t activate it yet. To do so, simply type:

setopt correct

The correction is not the only option that you can activate. After the “setopt” command, you can also use

  • beep: to play a beep sound when an error occurs
  • hist_ignore_all_dups: to prevent the same command from being recorded twice in the history
  • auto_cd: to move to a directory just by typing its name (no need of the cd command anymore)

On the contrary, if you want to delete an option, the syntax is:

unsetopt [option]

Another good use of Zsh is the recursive search. You were used with Bash to the operator “*” meaning “all”. Now with Zsh, “**” can be translated “all within all”. In other words, recursive search. Let me illustrate with an example. If you want to delete a file named “foobar” but you cannot remember where it is, this line will become handy:

rm **/foobar

Zsh will search for the foobar file within the current directory, and in every sub-directory, until it removes it.

The recursive operator can become really useful when searching a particular type of file. It is possible to use the combination of the “ls” command and a regex. A simple command like

ls **/*.mp3

will return all the mp3 files that Zsh can find recursively from the current directory.

Finally, let’s talk a little about the configuration of Zsh. As you know, it happens in ~/.zshrc. If you want to try some personalized prompts, Zsh comes with some saved themes. To see which one available, load the prompt system with

autoload -U promptinit
promptinit

And then list the themes with:

prompt -l

And to change your current prompt, type

prompt [theme's name]

If you have create plenty of aliases in bash, they won’t work in zsh. You have to port them over th the ~/.zsh file. The basic syntax is the same as Bash’s:

alias [alias' name]="[command]"

In addition, you can also define a specific alias for file extension. This allows you to automatically launch the appropriate command for the file that you want. For example, I use the program “mpg123” to play mp3’s from the command line. Therefore, I can add to my .zshrc:

alias -s mp3="mpg123"

From there, each time I write something like

./song.mp3

Mpg123 will be launched and play the song. With a little imagination, we can combine this functionality with my previous article on deal with archives from the command line:

alias -s tar="tar -xvf"
alias -s rar="unrar x"
alias -s zip="unzip"
alias -s pdf="evince"

Conclusion

Yes, Zsh and Bash are very similar, but zsh is more flexible that makes it more popular. Personally, I really appreciate the auto-completion feature and the alias for file extension. The above mentioned tips covered only a small part of what zsh can do. To find more about Zsh, I invite you to read the Archlinux wiki.

What do you think of the Zsh Shell ? Have you tried another one? Do you have any question concerning the shells in general? Please let us know in the comments.

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox

Sign up for all newsletters.
By signing up, you agree to our Privacy Policy and European users agree to the data transfer policy. We will not share your data and you can unsubscribe at any time. Subscribe


Adrien

Adrien is a young but passionate Linux aficionado. Command line, encryption, obscure distributions… you name it, he tried it. Always improving his system, he encountered multiple tricks and hacks and is ready to share them. Best things in the world? Math, computers and peanut butter!

Comments (4)