Bash 还是 KornShell (ksh)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/74844/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bash or KornShell (ksh)?
提问by user13158
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?
我对 *nix 并不陌生,但最近我花了很多时间在提示上。我的问题是使用 KornShell (ksh) 或 Bash Shell 的优势是什么?使用一个而不是另一个的陷阱在哪里?
Looking to understand from the perspective of a user, rather than purely scripting.
希望从用户的角度理解,而不是纯粹的脚本。
采纳答案by j.e.hahn
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.
各种 UNIX 和 Linux 实现有各种不同的 ksh 源级实现,其中一些是真正的 ksh,其中一些是 pdksh 实现,而其中一些只是指向具有“ksh”特性的其他 shell 的符号链接。这可能会导致执行行为的奇怪差异。
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.
至少对于 bash,你可以确定它是一个单一的代码库,你需要担心的是安装了什么(通常是最低)版本的 bash。在几乎所有现代(和不那么现代)的 UNIX 上编写了大量脚本后,根据我的经验,bash 编程更加可靠。
回答by David W.
The difference between Kornshell and Bash are minimal. There are certain advantages one has over the other, but the differences are tiny:
Kornshell 和 Bash 之间的区别很小。一个比另一个有某些优势,但差异很小:
- 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 theecho
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 replacesold
withnew
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 docd one two
and be done with it. In BASH, you'd have tocd ../../../../../two/bar/bar/foo/bar
.
- BASH 设置显示当前目录的提示要容易得多。在 Kornshell 中做同样的事情是骇人听闻的。
- Kornshell 有关联数组,而 BASH 没有。现在,我最后一次使用关联数组是......让我想想......从来没有。
- Kornshell 更好地处理循环语法。您通常可以在 Kornshell 循环中设置一个值,并在循环后使其可用。
- Bash 以更简洁的方式处理从管道中获取退出代码。
- Kornshell 有一个
print
比echo
命令更好的命令。 - Bash 有制表符补全。在旧版本中
- Kornshell 具有
r
历史命令,可让我快速重新运行旧命令。 - Kornshell 的语法在您的目录和 CD 中
cd old new
替换old
为new
。当您在一个名为的目录中/foo/bar/barfoo/one/bar/bar/foo/bar
并且需要 cd 到/foo/bar/barfoo/two/bar/bar/foo/bar
Kornshell 时,这很方便,您可以简单地cd one two
使用它并完成它。在 BASH 中,您必须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.
我是一个老 Kornshell 人,因为我在 1990 年代学习了 Unix,那时我选择了 shell。我可以使用 Bash,但有时我会感到沮丧,因为我习惯于使用 Kornshell 具有的一些小功能,而 BASH 没有并且它不起作用。因此,只要有可能,我都会将 Kornshell 设置为默认值。
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!.
但是,我要告诉你学习 BASH。Bash 现在已在大多数 Unix 系统和 Linux 上实现,并且与 Kornshell 相比,可用于学习 BASH 和获得帮助的资源更多。如果你需要在 BASH 中做一些奇特的事情,你可以在 Stackoverflow 上发布你的问题,几分钟后你会得到十几个答案——其中一些甚至是正确的!。
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.
如果您有 Kornshell 问题并将其发布在 Stackoverflow 上,您将不得不等待一些像我这样的老黑客从午睡中醒来,然后才能得到答案。而且,如果那天他们在养老院供应布丁,请忘记得到任何回应。
BASH is simply the shell of choice now, so if you've got to learn something, might as well go with what is popular.
BASH 现在只是首选的 shell,所以如果你必须学习一些东西,不妨选择流行的东西。
回答by Henk Langeveld
I'm a korn-shell veteran, so know that I speak from that perspective.
我是 korn-shell 老手,所以知道我是从这个角度说话的。
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.)
但是,我对 Bourne shell、ksh88 和 ksh93 很熟悉,而且我最了解哪些功能支持哪些功能。(我应该在这里跳过 ksh88,因为它不再广泛分发。)
For interactive use, take whatever fits your need. Experiment. I like being able to use the same shell for interactive use and for programming.
对于交互式使用,请选择适合您需要的任何内容。实验。我喜欢能够使用相同的 shell 进行交互使用和编程。
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.)
我从 SVR2 上的 ksh88 到 tcsh,再到 ksh88sun(增加了重要的国际化支持)和 ksh93。我尝试了 bash,但讨厌它,因为它使我的历史变得扁平。然后我发现shopt -s lithist
,一切都很好。(该lithist
选项确保换行符保留在您的命令历史记录中。)
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.
对于 shell 编程,如果您想要一致的编程语言、良好的 POSIX 一致性和良好的性能,我会认真推荐 ksh93,因为许多常见的 unix 命令可以作为内置函数使用。
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.
这将在不同的壳中产生不同的结果。korn-shell 从后到前运行管道;管道中的最后一个元素在当前进程中运行。Bash 直到 v4.x 才支持这种有用的行为,即便如此,它也不是默认行为。
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.
另一个说明一致性的例子:echo
命令本身由于 BSD 和 SYSV unix 之间的分裂而过时,并且每个都引入了自己的不打印换行符(和其他行为)的约定。这样做的结果仍然可以在许多“配置”脚本中看到。
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)
Ksh 对此采取了激进的方法 - 并引入了print
命令,该命令实际上支持两种方法(-n
来自 BSD的选项,以及\c
来自 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 以外的其他东西,例如 python、perl。或者更进一步,使用像 puppet 这样的平台——它允许您通过良好的审计观察和纠正整个系统集群的状态。
Shell programming is like swimming in uncharted waters, or worse.
Shell 编程就像在未知的水域中游泳,或者更糟。
Programming in any language requires familiarity with its syntax, its interfaces and behaviour. Shell programming isn't any different.
使用任何语言进行编程都需要熟悉其语法、接口和行为。Shell 编程也不例外。
回答by Kristian
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.
这有点像 Unix 与 Linux 之战。大多数(如果不是所有)Linux 发行版都安装了 bash 并且 ksh 是可选的。大多数 Unix 系统,如 Solaris、AIX 和 HPUX,都默认使用 ksh。
Personally I always use ksh, I love the vi completion and I pretty much use Solaris for everything.
就我个人而言,我总是使用 ksh,我喜欢 vi 完成,而且我几乎在所有事情上都使用 Solaris。
回答by Chris AtLee
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.
我没有使用 ksh 的经验,但我使用过 bash 和 zsh。我更喜欢 zsh 而不是 bash,因为它支持非常强大的文件通配符、变量扩展修饰符和更快的选项卡完成。
Here's a quick intro: http://friedcpu.wordpress.com/2007/07/24/zsh-the-last-shell-youll-ever-need/
这是一个快速介绍:http: //friedcpu.wordpress.com/2007/07/24/zsh-the-last-shell-youll-ever-need/
回答by Jon Ericson
For scripts, I always use kshbecause it smooths over gotchas.
对于脚本,我总是使用ksh因为它平滑了gotchas。
But I find bashmore comfortable for interactive use. For me the emacskey bindings and tab completion are the main benefits. But that's mostly force of habit, not any technical issue with ksh.
但我发现bash更适合交互式使用。对我来说,emacs键绑定和制表符完成是主要的好处。但这主要是习惯的力量,而不是ksh 的任何技术问题。
回答by Incident
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.
我的回答是“选择一个并学习如何使用它”。它们都是不错的壳;bash 可能有更多的花里胡哨,但它们都具有您想要的基本功能。bash 现在更普遍可用。如果您一直在使用 Linux,请坚持使用它。
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.
如果您正在编程,那么为了可移植性而尝试使用简单的 'sh' 是一种很好的做法,但是随着如今 bash 的广泛使用,这些建议可能有点过时了。
Learn how to use completion and your shell history; read the manpage occasionally and try to learn a few new things.
学习如何使用补全和你的 shell 历史;偶尔阅读联机帮助页并尝试学习一些新东西。
回答by Allen
For one thing, bash has tab completion. This alone is enough to make me prefer it over ksh.
一方面,bash 具有制表符完成功能。仅此一项就足以让我更喜欢它而不是 ksh。
Z shellhas a good combination of ksh's unique features with the nice things that bash provides, plus a lot more stuff on top of that.
Z shell将 ksh 的独特功能与 bash 提供的优秀功能完美结合,此外还有更多其他功能。
回答by Hank Gay
@foxxtrot
@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
.
实际上,标准的 shell 是 Bourne shell ( sh
)。/bin/sh
在 Linux 上实际上bash
是perl
.
回答by MeaCulpa
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.
在大多数 UNIX 系统中都可用,ksh 符合标准、设计清晰、全面。我认为书籍,ksh 中的帮助已经足够清晰,尤其是 O'Reilly 的书。Bash 是一种质量。我只在家里将它作为 Linux 的 root 登录 shell。
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.
对于交互式使用,我更喜欢 Linux/UNIX 上的 zsh。我在 zsh 中运行脚本,但我将在 AIX ksh 中测试我的大部分脚本和函数。