I'm looking to switch from bash to zsh but concerned about compatibility of bash scripts.

Are all bash scripts/functions compatible with zsh? Therefore, if that is true is zsh just an enhancement to bash?


If your scripts start with the line #!/bin/bash they will still be run using bash, even if your default shell is zsh.

I've found the syntax of zsh really close to the one of bash, and I did not pay attention if there was really some incompatibilities. I switched 6 years ago from bash to zsh seamlessly.

  • 1
    What was your most challenging part of switching over? – chrisjlee May 8 '12 at 20:36
  • 4
    None. My personal scripts add the proper reference to bash and I had found a good .zshrc to start with. Zsh and bash were enough similar that I did not really find it challenging. – Huygens May 8 '12 at 20:39
  • 4
    can you list your .zshrc :) – neaumusic Jun 3 '16 at 8:44
  • But if the line #!/bin/bash will be ignored if running the script file like source ./script.sh? – LCB Jun 13 '18 at 10:58
  • 2
    Hardg-coding the path to the shell is bad advice, even if it's done frequently. You should use #!/usr/bin/env bash instead, especially on macOS, where the default bash is severely outdated and new versions are virtually always installed into a different path. – Konrad Rudolph Jun 4 at 7:54

Zsh can run most Bourne, POSIX or ksh88 scripts if you put it in the right emulation mode (emulate sh or emulate ksh). It doesn't support all features of bash or ksh93. Zsh has most features of bash, but in many cases with a different syntax.

The shell you use interactively is irrelevant for any script you have. The shell that runs the scripts is the one indicated in the first line, the shebang line. For example, if the script starts with #!/bin/bash, it will be executed by bash.

If you've customized bash, you won't be able just rename your .bashrc to .zshrc. Some things can be shared, for example aliases and functions, as long as you stick to the intersection between the two shells (the intersection is close to ksh88 and pdksh). Other things, such as prompt settings, completion functions and most options, will need to be completely rewritten.

If you're writing a snippet for people to source from their .bashrc or .zshrc and you don't want to maintain two versions, stick to a common subset of bash and zsh features, which includes most of bash's programming features. Put your whole code in functions, and put the following line at the top of each function:

if [ -n "$ZSH_VERSION" ]; then emulate -L ksh; fi

You can use emulate sh instead of emulate ksh to be closer to plain sh syntax, which is what you need for .profile.

If a function calls another function, the other function inherits the emulate setting, so you don't need to put this line in internal functions, only in functions called by the end-user.

  • The shell you use is irrelevant if you run your scripts as ./my_script.sh. source my_script.sh and . my_script.sh will run it is as the current shell, ignoring any shebang. – BallpointBen Aug 29 '18 at 15:03

If the shebang is #!/bin/bash and you start the script as ./script the script will be executed by bash. Absolutely no problem here.

However, if you execute zsh ./script or source it . ./script to the running zsh instance, it is quite common that the syntax of bash and zsh won't match.

For example, zsh doesn't split parameter expansions by default, bash has a help builtin, there is no read -p prompt in zsh (the syntax is very different read cmd\?prompt, arrays start on 1 (not 0) in zsh,commandonly search for external commands in zsh, or there is no (simple) equivalent to${foo^}` (uppercase only first character) in zsh, among others. This is a long list of (mostly) similarities and some differences.

In some cases, zsh may be told to emulate other shells. In some cases, there is no common syntax portable to both shells possible (without using aliases or functions to emulate portable solutions).

However, zsh has many (a lot) of extensions that make easier to work interactively. That is at the same time an excellent reason to switch and a problem:

  1. Pro zsh

    • It is very nice to be able to see command syntax options pressing a tab.
    • Another major benefit of zsh is error correction when you make a typo. Rather than just display error: command not found, zsh will try to interpret what you tried to type. zsh will accept this input as a valid command.
    • Also, zsh has many modifiers to expansions that allow a large range of solutions. Like: list only files: ls *(.) (which is difficult with other shells). Even if when looking deeply enough the answer becomes also complex in zsh (print -rl -- *(/)).
    • Accept math with floats (with some caveats).
  2. Con zsh:

    • Bash is the default shell in many more systems.
    • Many zsh options doesn't help directly to writing bash compatible scripts.
    • It even could become a big problem trying to learn two shells at the same time.

In the end, it is your choice, and, I always like more choices.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.