Can I combine ksh/zsh and other shells with Bash in the same workflow (shell session)?

I am used to Bash but I might need some behavior available either in ksh or zsh. Could I use Bash with them, in a single shell session somehow, at least where they don't clash?

No problem to solve here, just a architecture question to better know relations between shells and for general knowledge.

  • I think what you are looking for is ksh -c COMMAND or zsh -c COMMAND – jesse_b Feb 25 '18 at 12:42

There are potentially two questions here:

  1. May I write scripts in multiple shell variants and use them together?
  2. May I write a script for one shell variant and have it executed by another shell interpreter?

For the first question:

Yes, as long as the scripts are executed by the shell interpreters that they were written for. Note that it doesn't matter what shell you use for interactive work as long as the scripts have proper #!-lines and/or are executed with explicit interpreters (e.g. ksh myscript).

A script works just like a command that you (or someone else) wrote. Another script could call it no matter in what language it was written (assuming it is executable and has a correct #!-line).

For the second:

If you write your scripts using only the lowest common denominator in terms of syntax and grammar (which in the case of these three shells is POSIX sh), you can be fairly certain that they will run under all these shells (but I'm assuming there may be edge cases where one shell's POSIX conformance may be lacking). But why would you have zsh or ksh run an sh script when you just as well could run it with /bin/sh?

ksh93 and bash (and possibly zsh as well, but I'm not too well versed in that shell) do offer a fair bit of commonality in their extensions to plain sh, but there are situations where even though the syntax and semantics are seemingly in agreement, strange issues arises from other differences.

In the general case, no, you can not assume that one shell will be able to successfully and correctly run a script written for another shell. For example, a bash script could use a ksh script just as if it was any other command, but it should never source a ksh script.

Ideally, you should assume that different shells are completely different scripting languages, and write for a particular shell, just as you would not write a piece of Python code and expect it to run under Perl or Ruby (unless it was some contrived polyglot script).

| improve this answer | |

As a simplistic example in the affirmative:

$ echo $SHELL
$ for othershell in ash dash posh rksh bash ksh zsh; 
    $othershell -c "echo Hello from the $othershell"; 
Hello from the ash
Hello from the dash
Hello from the posh
Hello from the rksh
Hello from the bash
Hello from the ksh
Hello from the zsh
| improve this answer | |

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.