我应该使用什么 Linux shell?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/199661/
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
What Linux shell should I use?
提问by Scottie T
I've used bash, csh, and tcsh. But I asked this question, and Jonathan informed me that csh isn't to be trusted. So what Linux shell is good for development. and why?
我使用过 bash、csh 和 tcsh。但是我问了这个问题,乔纳森告诉我 csh 不值得信任。那么Linux shell 有什么好开发的。为什么?
回答by warren
I usually stick to bash, because it's more friendly than straight-up sh, and it's the default on every distro I've used semi-regularly (SuSE, RHEL, Ubuntu, Slackware).
我通常坚持使用 bash,因为它比直接 sh 更友好,而且它是我半定期使用的每个发行版(SuSE、RHEL、Ubuntu、Slackware)的默认设置。
If you're planning to write portable shell scripts, however, make sure they all run in realsh.
但是,如果您打算编写可移植的 shell 脚本,请确保它们都在真正的sh 中运行。
回答by Kyle Burton
The most common shell, by far, on Linux is bash. Unless you have a good reason to use an alternative, I'd suggest that sticking with bash, or the most commonly used shell by your project team (or that the bulk of the shell scripts you have to work with) uses.
到目前为止,Linux 上最常见的 shell 是 bash。除非您有充分的理由使用替代方案,否则我建议您坚持使用 bash,或者您的项目团队最常用的 shell(或者您必须使用的大部分 shell 脚本)。
The only other very common contender is dash, which is becoming more widely used by the Ubuntu project.
唯一的另一个非常常见的竞争者是 dash,它正被 Ubuntu 项目更广泛地使用。
This really is personal preference, well, except for csh.
这确实是个人喜好,嗯,除了csh。
回答by Paul Nathan
Bash. It's standard.
猛击。这是标准的。
回答by TM.
Just don't use Korn Shell (ksh).
只是不要使用 Korn Shell (ksh)。
Unless you have perfect typing and never need to use the backspace key.
除非您有完美的打字能力并且永远不需要使用退格键。
回答by Jason Catena
I like ksh actually. It's a bit more consistent than bash because it does not try to support any csh constructs. tcsh, in my experience, is least compatible with other shells, and I avoid it. I try to write scripts to sh, but ksh does have some nice features like exporting and setting a variable on one line. I try to preserve compatibility with bash as well, since it is full-featured and common. To write portable shell scripts, which is more important than selecting the "best" shell, you might consult this book.
我实际上喜欢 ksh。它比 bash 更加一致,因为它不尝试支持任何 csh 结构。根据我的经验,tcsh 与其他 shell 的兼容性最低,我避免使用它。我尝试为 sh 编写脚本,但 ksh 确实具有一些不错的功能,例如在一行中导出和设置变量。我也尝试保持与 bash 的兼容性,因为它功能齐全且通用。要编写可移植的 shell 脚本,这比选择“最佳”shell 更重要,你可以参考这本书。
Portable Shell Programming: An Extensive Collection of Bourne Shell Examples (HP Professional Series) by Bruce Blinn (Paperback - Oct 29, 1995) amazon.com
便携式 Shell 编程:Bruce Blinn 的 Bourne Shell 示例(HP 专业系列)的广泛集合(平装书 - 1995 年 10 月 29 日) amazon.com
回答by SomeDork
I prefer zsh.
我更喜欢zsh。
The tab-completion alone is worth it:
单独的制表符完成是值得的:
- It expands wildcards if you want(handy when you want to delete all but one file in a directory)
- Will give you a list of switches after specifying a program
- Gives tab completion options below the line you're working on, which is pretty handy.
- 如果需要,它会扩展通配符(当您想删除目录中除一个文件外的所有文件时很方便)
- 指定程序后会给你一个开关列表
- 在您正在处理的行下方提供选项卡完成选项,这非常方便。
回答by james2vegas
I use pdksh all the time without having anything close to perfect typing (perhaps you need to fix your termcap?).
我一直使用 pdksh,但没有任何接近完美打字的东西(也许你需要修复你的 termcap?)。
ksh is the standard, csh is a standard and bash is 'standard', but on linux only. Better to target ksh.
ksh 是标准,csh 是标准,bash 是“标准”,但仅限于 linux。最好以 ksh 为目标。
回答by Jonathan Leffler
Since I believe I'm the person who suggested that you should use something other than C Shell, perhaps I should should qualify my remarks slightly, and then support those who said 'bash on Linux, Korn shell on other platforms (unless bash is installed there too)'.
既然我相信我是那个建议你应该使用 C Shell 以外的东西的人,也许我应该稍微限定一下我的言论,然后支持那些说 'Linux 上的 bash,其他平台上的 Korn shell(除非安装了 bash)那里也有)”。
Rather like editors (do you prefer vim
or emacs
), choice of shell is partly a question of familiarity and partly a question of preference. There are many who like C shell, though I do believe that it is less easily programmable than Bourne shell and derivatives. What I have in my .cshrc is, indeed, equivalent to exec /bin/ksh
(it isn't identically that because I want to execute a login shell - one that reads the profile and so on), but I wouldn't condemn anyone for using C shell or a derivative if it is an informed decision.
与编辑器(您喜欢vim
还是emacs
)一样,shell 的选择部分是熟悉的问题,部分是偏好的问题。有很多人喜欢 C shell,但我确实相信它不像 Bourne shell 和衍生产品那么容易编程。我在 .cshrc 中的内容确实等同于exec /bin/ksh
(这并不完全相同,因为我想执行一个登录 shell - 一个读取配置文件等等),但我不会谴责任何人使用 C shell或衍生品,如果是知情决定。
If you decide that you want to use something other than C shell, then you are basically in the Bourne shell camp, for which the POSIX standard more or less specifies the expected behaviour and then the different shells -- that is, the Bourne, Korn or Born Again shells -- add (or, in the case of the classic Bourne shell, subtract) a few features. If your code might ever need to move off Linux to HP-UX, Solaris or AIX (the surviving trio of the classic, AT&T-derived Unix variants), then you should consider ensuring you write your shell scripts in classic Bourne shell, though Korn shell is also pretty safe. Note, though, that on Linux you can write #!/bin/sh
and get Bash, on the other platforms, you will get Bourne shell.
如果您决定要使用 C shell 以外的其他东西,那么您基本上属于 Bourne shell 阵营,POSIX 标准或多或少指定了预期的行为,然后是不同的 shell——即 Bourne、Korn或 Born Again shell - 添加(或者,在经典的 Bourne shell 的情况下,减去)一些功能。如果您的代码可能需要从 Linux 迁移到 HP-UX、Solaris 或 AIX(经典的、AT&T 派生的 Unix 变体中幸存的三人组),那么您应该考虑确保使用经典的 Bourne shell 编写 shell 脚本,尽管 Korn shell也很安全。但是请注意,在 Linux 上您可以编写#!/bin/sh
并获得 Bash,在其他平台上,您将获得 Bourne shell。
I switch between Korn shell and Bash without major problems - and seldom with minor problems. I tend to stay clear of those corners of either language that are not well defined - which tends to mean 'defined in both'. Another problem for those using Linux is that the GNU tools have more options than the classic Unix versions, and you can lose portability not because of the shell programming constructs that you use but because of the command options you use. Experience and ready access to the manual pages of other systems helps enormously.
我在 Korn shell 和 Bash 之间切换没有大问题 - 很少有小问题。我倾向于避开任何一种语言的那些没有明确定义的角落——这往往意味着“在两者中都有定义”。对于使用 Linux 的人来说,另一个问题是 GNU 工具比经典的 Unix 版本有更多的选项,并且您可能会失去可移植性,不是因为您使用的 shell 编程结构,而是因为您使用的命令选项。经验和随时访问其他系统的手册页有很大帮助。
回答by Kirk Strauser
For interactivity, use Zsh. For a while I was the maintainer of the FreeBSD port of the Bash tab-completion scripts, but abandoned it as soon as I tried Zsh for the first time. It can do everything Bash can do but more easily and more elegantly. It also has the nice property of having extremely Bash-like keystrokes, so if you're on a system without Zsh, you'll be able to make do (even if it wouldn't "feel" as nice).
对于交互性,请使用 Zsh。有一段时间我是 Bash 选项卡完成脚本的 FreeBSD 端口的维护者,但在我第一次尝试 Zsh 时就放弃了它。它可以做 Bash 可以做的所有事情,但更容易、更优雅。它还具有非常类似于 Bash 的击键特性,因此如果您在一个没有 Zsh 的系统上,您将能够做到(即使它不会“感觉”那么好)。
For scripting, use Bourne Shell (sh). It's the POSIX standard scripting language and your scripts are pretty much guaranteed to work everywhere. Bash and Zsh and other shells have nice extensions that you'll miss but those tie you to a specific setup. Ignore that advice for personal-use-only scripts that you're certain you'll never run elsewhere, but remember that it's a real tradeoff that you need to consider.
对于脚本,请使用 Bourne Shell (sh)。它是 POSIX 标准脚本语言,您的脚本几乎可以保证在任何地方都可以使用。Bash 和 Zsh 以及其他 shell 有很好的扩展,您会错过这些扩展,但这些扩展会将您与特定设置联系起来。对于您确定永远不会在其他地方运行的仅供个人使用的脚本,请忽略该建议,但请记住,这是您需要考虑的真正权衡。
But in summary, Zsh. I don't know of anyone who's tried it who didn't immediately and permanently switch. It really is that good.
但总而言之,Zsh。我不知道有谁尝试过没有立即永久切换。真的那么好。
回答by Mark Baker
The problem with csh is that it's crap for scripting, as explained here. There's no real reason why you shouldn't use it as an interactive shell, but most people find it confusing having to learn two different shells and not being able to try out bits of their scripts on the command line, so it's easiest to use the same for everything.
csh 的问题在于它对于脚本编写来说是废话,正如这里所解释的。没有真正的理由为什么不应该将它用作交互式 shell,但是大多数人发现必须学习两个不同的 shell 并且无法在命令行上尝试他们的脚本的一部分令人困惑,因此最容易使用一切都一样。
The obvious candidates for an interactive shell are bash, dash, zsh and {pd,}ksh. All of these implement the posix shell standard, with some minor extensions. Pick whichever you like for interactive use, I'd tend to go with bash just because it's the standard on linux but they all have their merits and zsh in particular seems popular.
交互式 shell 的明显候选者是 bash、dash、zsh 和 {pd,}ksh。所有这些都实现了 posix shell 标准,并有一些小的扩展。选择任何你喜欢的交互式使用,我倾向于使用 bash,因为它是 linux 上的标准,但它们都有自己的优点,尤其是 zsh 似乎很受欢迎。
If you're writing a script that you intend to be portable, use #!/bin/sh, and make sure you use standard posix shell syntax. If it works on both bash and ksh it's probably standard. There are some old versions of unix which have a non-standard /bin/sh but I wouldn't bother with that unless you know you have to. More of a problem for portability are all the command line tools you call from your script.
如果您正在编写打算移植的脚本,请使用 #!/bin/sh,并确保您使用标准的 posix shell 语法。如果它适用于 bash 和 ksh,它可能是标准的。有一些旧版本的 unix 有一个非标准的 /bin/sh 但我不会打扰,除非你知道你必须这样做。可移植性的更多问题是您从脚本调用的所有命令行工具。