LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Whats the difference between ksh and bash? (https://www.linuxquestions.org/questions/linux-newbie-8/whats-the-difference-between-ksh-and-bash-791337/)

dev_d 02-24-2010 02:38 PM

Whats the difference between ksh and bash?
 
what are significant differences between ksh and bash? I read somewhere that ksh is proprietary shell and bash is completely open source.

rweaver 02-24-2010 02:42 PM

I actually had to look up some features between various shells recently that I don't frequently use and ran into this chart, it might answer some of your questions at least on the technical aspects.

http://www.unix.com/answers-frequent...nt-shells.html

MensaWater 02-24-2010 02:47 PM

There are open source versions of ksh. You can run it on Linux (we do). There was an older ksh for OSS called pdksh that acted somewhat differently than the newer ksh and that may be why folks didn't like it.

There are several differences - bash has most of what ksh does and then some extra stuff. 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

One thing that originally kept me from using bash over ksh was that I liked the vi style editing that ksh used by default but not bash. However, you can type "set -o vi" in bash to get that same functionality.

dev_d 02-25-2010 10:11 AM

Thanks jlightner

@rweaver - The link which u attached is having problem while opening.

rweaver 02-25-2010 11:02 AM

Odd, it opens from my net host, work machine, and home machine. Not sure what problem it might be having.

MensaWater 02-25-2010 12:33 PM

Quote:

Originally Posted by rweaver (Post 3876487)
Odd, it opens from my net host, work machine, and home machine. Not sure what problem it might be having.

It works for me too. Funny - I was going to provide the link and others but they are 7 years old or more. I have to believe there have been other enhancements to both ksh and bash since then.

dev_d 02-26-2010 08:08 AM

Thanks neways jlightner

catkin 02-26-2010 08:44 AM

More recent overviews of shells at KahKahKah and Wikipedia and a detailed comparison at Wikipedia.

GazL 02-26-2010 09:19 AM

Quote:

Originally Posted by jlightner (Post 3875500)
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


I prefer ksh in many ways, but I'm sure part of that is because it's what I'm used to being an old timer.

mesiol 02-26-2010 12:13 PM

Hi,

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.

MensaWater 02-26-2010 02:00 PM

Don't get me wrong. I've liked ksh ever since I first saw back in the early 90s. I was just saying there's a hell of a lot added to bash that doesn't exist in ksh or the posix sh.

I also agree that ksh is available on multiple platforms by default but bash would have to be specifically added. On the flip side though, most Linux distros come with bash and you have to add ksh if you want it.

For most of my scripting what I learned with ksh is sufficient so I do it that way (most of my math I do by piping to bc -l which works in both ksh and bash). However, I've seen enough other examples to let me know that bash can do some fairly sophisticated things of its own that ksh doesn't. The examples show indicate different ways of doing things in bash than ksh but didn't show anything that could be done in ksh that can't be done in bash in some way.

konsolebox 02-26-2010 04:17 PM

Quote:

Originally Posted by mesiol (Post 3878010)
Hi,

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.

but ksh is not installed by default on all linux at least..

Me I find bash's way of handling arrays easier and also the SIGCHILD signal (not sure if its there in KSH) is also important.


All times are GMT -5. The time now is 03:44 AM.