Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

zsh
bash

What is the difference between zsh and bash?

Jasmin Virdi

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Shell is an interface between the user and operating system services. It provides multiple features like background processing, input, and output redirection, aliasing, link, chain commands, and more, making the developers’ life easy. Here, we've curated the list of differences between zsh and bash.

So, let's compare these two shells on specific measures to determine which is more effective.

Performance

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

  • We've 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.

With bash:

time bash -c 'for i in $(seq 1 1000000);do [ 1 = 1 ];done'
Measure the performance of bash

With zsh:

Measure the performance of zsh
Measure the performance of zsh

We've written a script that reads input from the user and prints a statement multiple times using a for loop. This will help us 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

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.

Execution times
Execution times

The left and right outputs are for zsh and bash respectively. The results in both the above snippets show that zsh is faster than bash. The terms in the results mean 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 the .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.

An 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 are 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.

Differences

The processing of variables is the same in both zsh and bash. Let's assume we've a variable $var and both shells will take the value of var and split it at whitespace characters, and for each white space-separated part, if it contains wildcard characters and matches an existing file, it will replace the pattern with the list of matches. To just get the value of var , we need the following:

  • The length of an array in bash is from 0 to length-1 whereas it ranges from 1 to length in zsh.
  • Bash has an extra wildcard pattern which can be enabled by shopt -sextglob. In zsh, the same can be achieved by using setopt ksh_globor 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.

RELATED TAGS

zsh
bash

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring