0

So I was trying to create a script on bash shell, I came to know that the script doesn't run on ksh or dash shells. So my question is how you make a script to run on all 3 (bash, dash & ksh) shells.

3 Answers 3

2

In order to write a script that is guaranteed to be portable between the various shells, the script must be POSIX Shell compliant. POSIX is a minimum set of builtins and commands that all conforming shells must support. Ash, Dash, Zsh, Bash, Ksh, etc.. are all shells capable of running scripts that are POSIX compliant.

What shells like Bash do is add nice features which make the shell more capable, like additional parameter expansions for conversion to upper/lower case, substring replacement, etc.. and new builtins like [[ ... ]] that provide regex matching capabilities, etc.. While this makes Bash more capable, it also means scripts written using "Bashisms" are no longer able to run under all other shells. Ash, Dash and other minimal shells have no idea how to handle the features added by Bash, Ksh or Zsh and therefore fail.

To write truly portable scripts, you must limit the content to that provided by the POSIX command language.

Sign up to request clarification or add additional context in comments.

Comments

1

You need something file like this:

#!/bin/bash   #isn't a simple comment 
echo "hello bash"

#!/bin/sh     #isn't a simple comment 
echo "hello sh"

#!/bin/ksh    #isn't a simple comment 
echo "hello ksh"

( #!) it's called shebang tells the shell what program to interpret the script

called this file as you better prefert (file.bsk), but don't forget give it execute permission it with :

chmod +x file.bsk

then run ./file.bsk

Comments

0

Some commands or utilities are not available in all shells or they might have different behavior in different shell. If you know which command run on which shell or gives you desired output you can write shell specific commends as below

bash -c 'echo bash'

ksh -c 'echo ksh'

All other commands that are common to all shell can be written in normal way.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.