71

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.

javaPlease42
  • 4,699
  • 7
  • 36
  • 65
user13158
  • 721
  • 1
  • 6
  • 4

12 Answers12

141

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.

Community
  • 1
  • 1
David W.
  • 105,218
  • 39
  • 216
  • 337
  • 8
    For the sake of completeness, bash version 4 supports associative arrays. I've never used AAs in either shell, so I can't say if they work better, as well as, or worse than Kornshell's implementation. – chepner Feb 02 '12 at 20:29
  • 4
    "You can usually set a value in a Kornshell loop and have it available after the loop." -- i find that confusing as hell. i certainly support that a variable should have the scope only within its level of nesting and not outside of it. – amphibient Feb 22 '13 at 19:42
  • 3
    It has to do with piping a command to a `while read`loop. Kornshell has no problems with accessing the variable outside the loop. However, for BASH, that pipe is a child process, and you lose the value once the loop is done. There are ways to get around this. – David W. Feb 22 '13 at 20:54
  • Also, bash implements `cd old new` as `^old^new` and can do this not only on directories/ – Dmytro Sirenko Jul 16 '13 at 10:30
  • 3
    @EarlGray That doesn't work unless the previous command was my change directory. Someone else pointed out I could do this `$ cd ${PWD/foo/bar}` but that's a bit long to type. – David W. Jul 16 '13 at 17:15
  • In Bash you can rerun history commands by typing in !N where N is the line number. – Dave Jul 22 '13 at 16:10
  • @Dave It's not as convenient as `r vi` where it will run my last VI command. I usually don't know the history line number, and I usually set `extglob` (a feature that's on by default in Ksh and I heavily depend upon) which kills the use of `!` in command history. – David W. Jul 22 '13 at 17:03
  • Just want to add, all the Kornshellisms I mentioned are _nice to have_ features I've grown dependent upon. If I learned shell today, it would be BASH, and wouldn't understand all the whining by the Kornshell old timers talking about this or that feature and how much better the Internet was back in the old days. It would be like listening to your grandpa whine about how much better things were when he was a kid. Come to think it it, it is your grandpa whining about how much better things were when he was a kid. (_When I was a kid, there were only three TV channels, and we liked it!_) – David W. Jul 22 '13 at 17:07
  • `^Rvi` (without space) reruns vim. Vim ?! Use `:color desert` in .vimrc when you have `:syntax on`. – Walter A Feb 16 '15 at 08:54
  • 6
    In bash, `!vi` will run the last run command that started with the letters 'vi'. If you're not sure if that command was the one you want, use `!vi:p` which will print the command and add it to your history list newly—but not actually run it. Then if it *is* what you want, a simple up arrow-enter, or just `!!` to run it. See `man bash` and search for `HISTORY EXPANSION`. – Wildcard Oct 16 '15 at 07:17
  • I also think it's important when learning bash to keep very clear track of which options are bash-specific and which are POSIX compatible, and try to make the POSIX compatible approaches your habit. If you use bash-only features you should *know* you are using them. Keeping them mentally separate from the beginning is a very good idea. – Wildcard Oct 16 '15 at 07:23
  • Much of this is outdated. bash has had associative arrays since 4.0 (and they're **hugely** useful -- would you try programming in Python without dicts?), and namevars since 4.3. On the other hand, genuine David Korn ksh93 still has a **huge** edge in performance -- things one needs to use `awk` for for pure performance reasons in bash (think about textual transformations or analysis of a multi-GB stream) can be done in ksh93 natively with comparable speed. – Charles Duffy Sep 18 '16 at 06:05
  • Oh -- and ksh93 has floating-point math, which bash still lacks. – Charles Duffy Sep 18 '16 at 06:08
  • About the 5th point, you can simply do `alias print=echo` – Jerfov2 Oct 24 '16 at 23:23
  • 1
    Thanks for this answer, it made me laugh :D (and was useful) – niels Dec 07 '17 at 09:13
  • in bash, `$ cd -` takes you to your `$OLDPWD` – Jethro Cao Mar 22 '21 at 21:32
48

Bash.

The various UNIX and Linux implementations have various different source level implementations of ksh, some of which are real ksh, some of which are pdksh implementations and some of which are just symlinks to some other shell that has a "ksh" personality. This can lead to weird differences in execution behavior.

At least with bash you can be sure that it's a single code base, and all you need worry about is what (usually minimum) version of bash is installed. Having done a lot of scripting on pretty much every modern (and not-so-modern) UNIX, programming to bash is more reliably consistent in my experience.

j.e.hahn
  • 512
  • 4
  • 2
  • 4
    If you're doing only minimum-common-denominator ksh, I'd agree. Genuine David Korn ksh93, on the other hand, has compelling advantages for scripting: Speed that puts it near `awk` for text processing; native floating point math; namevars and associative arrays even on systems not running the latest release; etc. – Charles Duffy Sep 18 '16 at 06:10
35

I'm a korn-shell veteran, so know that I speak from that perspective.

However, I have been comfortable with Bourne shell, ksh88, and ksh93, and for the most I know which features are supported in which. (I should skip ksh88 here, as it's not widely distributed anymore.)

For interactive use, take whatever fits your need. Experiment. I like being able to use the same shell for interactive use and for programming.

I went from ksh88 on SVR2 to tcsh, to ksh88sun (which added significant internationalisation support) and ksh93. I tried bash, and hated it because it flattened my history. Then I discovered shopt -s lithist and all was well. (The lithist option assures that newlines are preserved in your command history.)

For shell programming, I'd seriously recommend ksh93 if you want a consistent programming language, good POSIX conformance, and good performance, as many common unix commands can be available as builtin functions.

If you want portability use at least both. And make sure you have a good test suite.

There are many subtle differences between shells. Consider for example reading from a pipe:

b=42 && echo one two three four |
    read a b junk && echo $b

This will produce different results in different shells. The korn-shell runs pipelines from back to front; the last element in the pipeline runs in the current process. Bash did not support this useful behaviour until v4.x, and even then, it's not the default.

Another example illustrating consistency: The echo command itself, which was made obsolete by the split between BSD and SYSV unix, and each introduced their own convention for not printing newlines (and other behaviour). The result of this can still be seen in many 'configure' scripts.

Ksh took a radical approach to that - and introduced the print command, which actually supports both methods (the -n option from BSD, and the trailing \c special character from SYSV)

However, for serious systems programming I'd recommend something other than a shell, like python, perl. Or take it a step further, and use a platform like puppet - which allows you to watch and correct the state of whole clusters of systems, with good auditing.

Shell programming is like swimming in uncharted waters, or worse.

Programming in any language requires familiarity with its syntax, its interfaces and behaviour. Shell programming isn't any different.

Henk Langeveld
  • 8,088
  • 1
  • 43
  • 57
  • by "interactive use" do you mean manual prompt-to-prompt command execution as opposed to running scripts that bundle such commands? – amphibient Feb 22 '13 at 19:39
  • 3
    @foampile Yes, that's what "interactive use" means. – Telemachus Feb 23 '13 at 13:21
  • 2
    Re: your `read` difference, in noninteractive mode that's actually configurable for modern bash -- see the `lastpipe` option. – Charles Duffy Sep 22 '16 at 16:41
  • @CharlesDuffy correct, and it was available when I wrote this - however, it's still not default behaviour - you need bash 4.2 or 4.3 and the lastpipe shopt. Shopt options are awkward, as their setting can actually trigger syntax errors. – Henk Langeveld Sep 23 '16 at 08:45
7

This is a bit of a Unix vs Linux battle. Most if not all Linux distributions have bash installed and ksh optional. Most Unix systems, like Solaris, AIX and HPUX have ksh as default.

Personally I always use ksh, I love the vi completion and I pretty much use Solaris for everything.

Kristian
  • 111
  • 2
  • HP-UX provides sh, csh, and ksh as default shells. In 11.31 bash was included on the default installation media. But, given the amount of 10.20 still running in the world, ksh is still a better bet for administering HP-UX. – Stan Graves Jul 07 '09 at 15:27
  • You can use vi completion in bash with `set -o vi`, and add it to your `~/.bashrc` file to make it persistent. That's how I have mine set. – Wildcard Oct 16 '15 at 07:19
6

For scripts, I always use ksh because it smooths over gotchas.

But I find bash more comfortable for interactive use. For me the emacs key bindings and tab completion are the main benefits. But that's mostly force of habit, not any technical issue with ksh.

Community
  • 1
  • 1
Jon 'links in bio' Ericson
  • 20,880
  • 12
  • 98
  • 148
5

I don't have experience with ksh, but I have used both bash and zsh. I prefer zsh over bash because of its support for very powerful file globbing, variable expansion modifiers, and faster tab completion.

Here's a quick intro: http://friedcpu.wordpress.com/2007/07/24/zsh-the-last-shell-youll-ever-need/

Chris AtLee
  • 7,798
  • 3
  • 28
  • 27
4

For one thing, bash has tab completion. This alone is enough to make me prefer it over ksh.

Z shell has a good combination of ksh's unique features with the nice things that bash provides, plus a lot more stuff on top of that.

Allen
  • 5,034
  • 22
  • 30
3

@foxxtrot

Actually, the standard shell is Bourne shell (sh). /bin/sh on Linux is actually bash, but if you're aiming for cross-platform scripts, you're better off sticking to features of the original Bourne shell or writing it in something like perl.

Hank Gay
  • 70,339
  • 36
  • 160
  • 222
  • Note that bash has different behaviour if /bin/sh is (sym)linked to bash: If bash is invoked with the name sh, it tries to mimic the startup behavior of historical versions of sh as closely as possible, while conforming to the POSIX standard as well. – glenn jackman Apr 02 '11 at 22:51
  • 4
    On Debian the standard `/bin/sh` is actually `dash`, the debian almquist shell. This takes a lot of people by surpise when their "valid" bash commands are rejected by `#!/bin/sh` – thom Nov 23 '13 at 00:46
3

My answer would be 'pick one and learn how to use it'. They're both decent shells; bash probably has more bells and whistles, but they both have the basic features you'll want. bash is more universally available these days. If you're using Linux all the time, just stick with it.

If you're programming, trying to stick to plain 'sh' for portability is good practice, but then with bash available so widely these days that bit of advice is probably a bit old-fashioned.

Learn how to use completion and your shell history; read the manpage occasionally and try to learn a few new things.

Incident
  • 489
  • 4
  • 7
  • 1
    As of this answer's writing, ksh93 arguably had more bells and whistles from a programming perspective (associative arrays and namevars come first to mind) -- though that's changed in the intervening eight years. It still has floating-point math, which bash continues to lack. – Charles Duffy Sep 18 '16 at 06:06
2

Available in most UNIX system, ksh is standard-comliant, clearly designed, well-rounded. I think books,helps in ksh is enough and clear, especially the O'Reilly book. Bash is a mass. I keep it as root login shell for Linux at home only.

For interactive use, I prefer zsh on Linux/UNIX. I run scripts in zsh, but I'll test most of my scripts, functions in AIX ksh though.

MeaCulpa
  • 881
  • 1
  • 6
  • 14
-1

Bash is the benchmark, but that's mostly because you can be reasonably sure it's installed on every *nix out there. If you're planning to distribute the scripts, use Bash.

I can not really address the actual programming differences between the shells, unfortunately.

foxxtrot
  • 11,214
  • 4
  • 27
  • 27
  • 2
    If you are planning to distribute scripts use posix-sh. Bash is a good shell, and the default on linux. Older Unix flavors tend to have sh, ksh, and csh. In a production environment, it can be difficult to get permission or forgiveness for installing the shell of your choice on all the systems. – Stan Graves Jul 07 '09 at 15:25
  • 2
    It's not installed by default on the AIX we use at work (major US financial institution), and boy do I wish it was. – Brian Warshaw Jul 05 '11 at 18:14
  • Sorry, but Bash is NOT "installed on every *nix out there". Although it is probably on every GNU/Linux distro. If you are supporting UNIX or you want to follow POSIX then learn & use ksh, because ksh88 was (in part) what the sh standard was based on. If you are only on GNU/Linux and don't care about compatibility with legacy systems then Bash is fine. Most of the features are comparable between the two, but Bash makes it too easy to violate POSIX for my taste. Even when you tell it specifically not to, it usually lets you anyway. The vi mode in Bash is also pretty poorly implemented. – Kajukenbo Oct 29 '21 at 04:30
-2

Bash is the standard for Linux.
My experience is that it is easier to find help for bash than for ksh or csh.

Matthieu
  • 628
  • 5
  • 8