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