Bash or KornShell (ksh)?

user13158 picture user13158 · Sep 16, 2008 · Viewed 108.7k times · Source

I am not new to *nix, however lately I have been spending a lot of time at the prompt. My question is what are the advantages of using KornShell (ksh) or Bash Shell? Where are the pitfalls of using one over the other?

Looking to understand from the perspective of a user, rather than purely scripting.

Answer

David W. picture David W. · Aug 8, 2011

The difference between Kornshell and Bash are minimal. There are certain advantages one has over the other, but the differences are tiny:

  • BASH is much easier to set a prompt that displays the current directory. To do the same in Kornshell is hackish.
  • Kornshell has associative arrays and BASH doesn't. Now, the last time I used Associative arrays was... Let me think... Never.
  • Kornshell handles loop syntax a bit better. You can usually set a value in a Kornshell loop and have it available after the loop.
  • Bash handles getting exit codes from pipes in a cleaner way.
  • Kornshell has the print command which is way better than the echo command.
  • Bash has tab completions. In older versions
  • Kornshell has the r history command that allows me to quickly rerun older commands.
  • Kornshell has the syntax cd old new which replaces old with new in your directory and CDs over there. It's convenient when you have are in a directory called /foo/bar/barfoo/one/bar/bar/foo/bar and you need to cd to /foo/bar/barfoo/two/bar/bar/foo/bar In Kornshell, you can simply do cd one two and be done with it. In BASH, you'd have to cd ../../../../../two/bar/bar/foo/bar.

I'm an old Kornshell guy because I learned Unix in the 1990s, and that was the shell of choice back then. I can use Bash, but I get frustrated by it at times because in habit I use some minor feature that Kornshell has that BASH doesn't and it doesn't work. So, whenever possible, I set Kornshell as my default.

However, I am going to tell you to learn BASH. Bash is now implemented on most Unix systems as well as on Linux, and there are simply more resources available for learning BASH and getting help than Kornshell. If you need to do something exotic in BASH, you can go on Stackoverflow, post your question, and you'll get a dozen answers in a few minutes -- and some of them will even be correct!.

If you have a Kornshell question and post it on Stackoverflow, you'll have to wait for some old past their prime hacker like me wake up from his nap before you get an answer. And, forget getting any response if they're serving pudding up in the old age home that day.

BASH is simply the shell of choice now, so if you've got to learn something, might as well go with what is popular.