DEV Community

Jasmin Virdi
Jasmin Virdi

Posted on

Zsh vs Bash A brief difference between zsh and bash

Shell is an interface between the user and operating system services. It provides multiple features like background processing, input and output redirection, aliasing, link, and chain commands, etc. which makes the life of developers easy. I have started using zsh for quite some time and found it more effective than bash. So here I have curated the list of differences between the two which has helped me a lot to understand.

So let's compare these two shells on certain basis and find out the differences.

Performance

It is one of the useful means of benchmarking the shell's performance. To evaluate this let's generate some load and check how fast the shell responds.

  • I have compared the speed of both the shell once invoked by running the following command:

time shell_name -c 'for i in $(seq 1 1000000);do [ 1 = 1 ];done'

In the above command replace shell_name with zsh and bash.

Alt Text

  • I have written a script that reads input from the user and prints in for loop multiple times. This will help to check the time taken to process the script.
#!/bin/bash
# Script name: test.sh
for i in `seq 1 100`
do
    for var in $* 
    do
        echo "command line contains: $var"
    done
done
Enter fullscreen mode Exit fullscreen mode

Run the following command in both the shell to see the script execution time in each shell.

time shell_name ./test.sh "some_long_sentance"

Replace the shell_name with zsh/bash and some long sentences in the above command.

Alt Text
The left and right outputs are for zsh and bash respectively.
The results in both the above snippets shows that zsh is faster than bash. The terms in the results means the following:

  • real is the time from start to finish of the call.
  • user is the amount of CPU time spent in user-mode within the process.
  • sys is the amount of CPU time spent in the kernel within the process.

Configuration Files

Bash reads .bashrc file in non login interactive shell and .bash_profile in login shells.

Zsh reads .zshrc in an interactive shell and .zprofile in a login shell.

Interactive shell is a simple shell that drives input from the user and returns the desired output.
A login shell is the first process that executes under our user ID when we log in to a session.

Key Bindings

Any keyboard actions which are done while typing the commands on the command line like pressing Ctrl+E to move to the end of the line is called key bindings. It uses a completely different syntax.

Bash uses .inputrc and bind builtIn to bind keys to readline commands.

Zsh uses bindkey builtIn to bind keys to zle widgets.

Prompt

Bash sets the prompt from PS1 which contains backslash escapes like \a whereas zsh contains the percent escapes like %d. The functionality of bash PROMT_COMMAND is available in zsh via precmd.
Zsh also offers ways to do fancy customizations.

Completion

Both the shells provide features like command completion and switching to fancy mode. In bash it is done by including bash_completion while zsh achieves this by running compinit.
More about zsh Completion System.

Scripting differences

  • The processing of variables is the same in both zsh and bash. Let's
    assume we have a variable $var, both the shells will take the value of
    var splits it at whitespace characters, and for each whitespace-
    separated part, if it contains wildcard characters and matches an
    existing file replaces the pattern by the list of matches. To just get
    the value of var we need $var

  • Length of an array in bash is from 0 to length-1 whereas it ranges from
    1 to length in zsh.

  • Bash has extra wildcard pattern which can be enabled by shopt -s
    extglob
    . In zsh same can be achieved by using setopt ksh_glob
    or setopt extended_globfor simpler to type native syntax.

There are some nice zsh features like Glob Qualifiers and widcard patterns that bash doesn't offer. There are many zsh configuration frameworks on the web. They can be a convenient way to get started with some powerful features.

Top comments (15)

Collapse
 
android_human7 profile image
Anshuman

For me the biggest gain of Zsh is not the speed (I can't convince a fellow developer to save a few milliseconds every day :-P ) but the fact that it comes it themes and customizations. The Spaceship theme for example has been a real favourite, because it's easier on the eyes for long term use AND it actually saves me time due to the colour combinations giving me perspective faster.

Collapse
 
jasmin profile image
Jasmin Virdi • Edited

I love zsh themes. There are plenty to choose from and each has amazing colour contrast. I am using Avit will try Spaceship theme. It is a difference of few milliseconds but it is taking just twice the time and this can be useful when performing heavy operations !

Collapse
 
waylonwalker profile image
Waylon Walker

I've been using the starship prompt lately. Takes care of everything I need out of the box.

Collapse
 
jasmin profile image
Jasmin Virdi

With starship, we don't have to worry much about the shell as it works on top of it and provides easy customizations. :)

Collapse
 
codenutt profile image
Jared

Spaceship is the beeeeeest

Collapse
 
thefluxapex profile image
Ian Pride • Edited

I keep getting the opposite of your benchmark results.
I'm on:

 $ lsb_release -a
Distributor ID: Ubuntu
Description:    Ubuntu 20.04.1 LTS
Release:        20.04
Codename:       focal
 $ uname -srp
Linux 5.4.0-54-generic x86_64
Enter fullscreen mode Exit fullscreen mode

Screen

Collapse
 
_deekshithanand profile image
Deekshith Anand • Edited

I switched to zsh after this! ZSH with starship theme, syntax-highlighting and auto-suggestions features is simply the best! The emojis in the prompt is awesome and more human haha! Here's a prompt with emojis!!!

Collapse
 
jasmin profile image
Jasmin Virdi

Totally agreed πŸ™ŒπŸ»

I am also using zsh with starship and it works great for me too. πŸ˜‡

Collapse
 
shaunakde profile image
Shaunak De • Edited

I'm not sure I'd conclusively say zsh is faster. Yes, it takes less user time to execute your scripts. But the CPU load (sys) time is an order of magnitude less. It's probably preempting the CPU more, resulting in a longer user time, but lower system load. In anycase, yes, if the speed of the shell script in total time matters, zsh is good. But say you're on a VM or embedded system with 128MB of ram and a weak CPU... I'd rather run a long running watch on bash knowing it wouldn't furthur slow the process.

However, the customization/color options for zsh combined with the feature set are definately superior. This article has definately convinced my to atleast give zsh a shot.

The only downside for me is that I have hundreds of bash scripts around my codebase that were written to take advantage of bashisms and they would encounter errors. (Unless ofcourse I went in and explicitly added #! /bin/bash to each of these)

Collapse
 
waylonwalker profile image
Waylon Walker

I use zsh as my default shell everywhere. I love the ease of setting up an immensely better developer experience while using it. I still write all of my scripts to run in sh if possible otherwise bash.

Collapse
 
yunielrc profile image
Yuniel • Edited

zsh faster than bash?

zsh/bash/dash

Collapse
 
jasmin profile image
Jasmin Virdi

I could sense the possible reason behind this might be the sourcing of a few other files in the .zshrc. Sometimes it is also because of the plugins which are unable to find the command. I suggest you should find the reason why it is taking a long time.
You can try the following steps, they might help you in detecting the root cause.

  1. To check the xtrace and verbose output you can try the command zsh -xv
  2. Second you can try an order-blind profiler which will enable built-in profiling. For this you need to add zmodload zsh/zprof and zprof at the top and bottom of your .zshrc file. This will tell you where zsh is taking more time.
  3. There can be few dependencies which slow down the shell's performance. For that, there is a great sandbox which will helps you to speed up the shell's performance in case this the culprit behind slowing down your shell.
Collapse
 
yunielrc profile image
Yuniel • Edited

Hi Jasmin, thanks for your reply, bash is faster than zsh in linux.

you can see the video below:
zsh vs bash performance in ubuntu 20.04 server

Thread Thread
 
jasmin profile image
Jasmin Virdi

Thanks for sharing the video!πŸ‘

Thread Thread
 
yunielrc profile image
Yuniel

A pleasure, have a nice day Jasmin