Tuesday, February 26, 2013

Found some more matches in linuxquestions.org when searching for differences between ksh and bash

out of these the one by GazL is what caught my attention

Quote:
Originally Posted by jlightner View Post
A script written to run in ksh would likely run in bash just fine. A lot of the differences deal with math, variables, arrays, functions etc... - bash just seems to have a hell of lot more of this than ksh
bash isn't always the winner. ksh has some nice useful variable typing that bash doesn't.
Code:
$ typeset -l lowercase_only
$ lowercase_only="WiBbLe"
$ echo $lowercase_only   
wibble
It's much easier than
Code:
lowercase_only=$(echo "WiBbLe" | tr "[:upper:]" "[:lower:]")
... and you don't have to keep reapplying the translation to ensure the value is lowercase should the value ever be changed. Very Useful for when dealing with user input.


I also prefer the ksh math syntax of
Code:
let a=b+c
rather than bash's more unwieldy;
Code:
a=$(( b+c ))
... but that's just a minor style thing.


The biggest difference that tends to catch people out though is this one...

ksh:
Code:
$ echo wibble | read variable
$ echo $variable
wibble
$
bash:
Code:
bash-3.1$ echo wibble | read variable
bash-3.1$ echo $variable

bash-3.1$
... Oi! bash!... where'd my variable go?
What makes this one particularly bad is that the bash code looks like it ought to work. It's only when you understand the underlying semantics of how bash deals with pipelines that you realise why it doesn't.

Because bash runs a pipeline in a sub-shell, you actually have to do it like this 
Code:
read variable < <(echo wibble)
echo $variable

mesiol also has a point

one reason for using ksh for scripting is, this shell is available on nearly all existing flavours of *nix. Bash is not installed by default on all *nix.

No comments:

Post a Comment