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?

up vote 39 down vote accepted

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 yeras ago from bash to zsh seemlessly.

  • What was your most challenging part of switching over? – chrisjlee May 8 '12 at 20:36
  • 3
    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
  • 3
    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 at 10:58
  • @LCB I guess so. But I usually run my scripts without invoking source. I only source “scripts” which are more configuration file (e.g. export, alias, etc.). Those need some adaptations if they contain functions or bashism. Btw, I’m using now fish instead of zsh. – Huygens Jun 13 at 14:23

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 at 15:03

But sometimes, there is incompability and may cause danger(This made me confused last night). So just ensure you have "#!/usr/bin/env bash" at the first line.

This passage will help a lot. http://slopjong.de/2012/07/02/compatibility-between-zsh-and-bash/

  • 4
    Welcome to Unix & Linux! Generally we like answers on the site to be able to stand on their own - Links are great, but if that link ever breaks the answer should have enough information to still be helpful. Please consider editing your answer to include more detail. See the FAQ for more info. – slm Apr 6 '13 at 1:22
  • The conditional section on that page is very confused - it omits the crucial &&, as one of the comments points out. – RichVel Oct 20 '17 at 15:58

Your Answer


By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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