bash 值得切换到 zsh 以供随意使用吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43321/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 17:35:47  来源:igfitidea点击:

Worth switching to zsh for casual use?

bashshellzsh

提问by Will Robertson

The default shell in Mac OS X is bash, which I'm generally happy to be using. I just take it for granted. It would be really nice if it auto-completed more stuff, though, and I've heard good things about zshin this regard. But I don't really have the inclination to spend hours fiddling with settings to improve my command line usage by a tiny amount, since my life on the command line isn't that bad.

Mac OS X 中的默认 shell 是bash,我通常很乐意使用它。我只是认为这是理所当然的。不过,如果它能自动完成更多的东西,那就太好了,而且我听说过zsh这方面的好消息。但我真的不想花几个小时摆弄设置以稍微提高我的命令行使用率,因为我在命令行上的生活并没有那么糟糕。

(As I understand it, bashcan also be configured to auto-complete more cleverly. It's the configuring I'm not all that keen on.)

(据我所知,bash也可以配置为更巧妙地自动完成。这是我不太热衷的配置。)

Will switching to zsh, even in a small number cases, make my life easier? Or is it only a better shell if you put in the time to learn whyit's better? (Examples would be nice, too :))

zsh即使在少数情况下,切换到也会让我的生活更轻松吗?或者,如果您花时间了解为什么它更好,它只是更好的外壳吗?(例子也很好:)



@Rodney Amato& @Vulcan Eagergive two good reasons to respectively stick to bashand switch to zsh. Looks like I'll have to investigate both! Oh well :)

@罗德尼·阿马托&@火神伊格举两个很好的理由,分别坚持bash和开关zsh。看来我得调查这两个了!那好吧:)

Is there anyone with an opinion from both sides of the argument?

有没有人对争论的双方都有意见?

采纳答案by Rodney Amato

For casual use you are probably better off sticking with bash and just installing bash completion.

对于休闲使用,您最好坚持使用 bash 并只安装 bash 完成功能。

Installing it is pretty easy, grab the bash-completion-20060301.tar.gz from http://www.caliban.org/bash/index.shtml#completionand extract it with

安装它非常简单,从http://www.caliban.org/bash/index.shtml#completion获取 bash-completion-20060301.tar.gz并用

tar -xzvf bash-completion-20060301.tar.gz

then copy the bash_completion/bash_completion file to /etc with

然后将 bash_completion/bash_completion 文件复制到 /etc 中

sudo cp bash_completion/bash_completion /etc

which will prompt you for your password. You probably will want to make a /etc/bash_completion.d directory for any additional completion scripts (for instance I have the git completion script in there).

这将提示您输入密码。您可能希望为任何其他完成脚本创建一个 /etc/bash_completion.d 目录(例如,我在那里有 git 完成脚本)。

Once this is done the last step is to make sure the .bash_profile file in your home directory has

完成此操作后,最后一步是确保主目录中的 .bash_profile 文件具有

if [ -f /etc/bash_completion ]; then
     . /etc/bash_completion 
fi

in it to load the completion file when you login.

登录时加载完成文件。

To test it just open a new terminal, and try completing on cvs and it should show you the cvs options in the list of completions.

要测试它,只需打开一个新终端,然后尝试在 cvs 上完成,它应该会在完成列表中显示 cvs 选项。

回答by Matt

Personally, I love zsh.

就个人而言,我喜欢 zsh。

Generally, you probably won't notice the difference between it and bash, until you want to quickly do things like recursive globbing:

通常,您可能不会注意到它和 bash 之间的区别,除非您想快速执行递归通配符之类的操作:

  • **/*.cfor example.
  • **/*.c例如。

Or use suffix aliases to associate specific progs with different suffixes, so that you can "execute" them directly. The below alias lets you "run" a C source file at the prompt by simply typing ./my_program.c– which will work exactly as if you typed vim ./my_program.c. (Sort of the equivalent to double clicking on the icon of a file.)

或者使用后缀别名将特定的 prog 与不同的后缀相关联,以便您可以直接“执行”它们。下面的别名使您可以在提示符下通过简单地键入“运行”C 源文件./my_program.c- 这将与您键入vim ./my_program.c. (相当于双击文件图标。)

  • alias -s c=vim
  • alias -s c=vim

Or print the names of files modified today:

或者打印今天修改的文件名:

  • print *(e:age today now:)
  • print *(e:age today now:)

You can probably do all of these things in bash, but my experience with zsh is that if there's something I want to do, I can probably find it in zsh-lovers. I also find the book 'From Bash to Z-Shell' really useful.

您可能可以在 bash 中完成所有这些事情,但我对 zsh 的经验是,如果有我想做的事情,我可能可以在zsh-lovers 中找到它。我还发现“从 Bash 到 Z-Shell”这本书非常有用。

Playing with the mind bogglingly large number of options is good fun too!

用脑子玩大量的选项也很有趣!

回答by Rodney Amato

Switch to zsh. You will have access to:

切换到 zsh。您将可以访问:

  1. zmv: You can do: zmv '(*).mp3' '$1.wma'for thousands of files.
  2. zcalc: Extremely comfortable calculator, better than bc.
  3. zparseopts: One-liner for parsing arbitrary complex options given to your script.
  4. autopushd: You can always do popdafter cdto change back to your previous directory.
  5. Floating point support. It is needed from time to time.
  6. Hashes support. Sometimes they are just a key feature.
  1. zmv: 你可以做:zmv '(*).mp3' '$1.wma'数以千计的文件。
  2. zcalc: 非常舒适的计算器,比bc.
  3. zparseopts:用于解析提供给脚本的任意复杂选项的单行程序。
  4. autopushd:您可以随时做popdcd改回以前的目录。
  5. 浮点支持。不时需要它。
  6. 哈希支持。有时它们只是一个关键功能。

回答by jkramer

If all you want to use ZSH for is better completion, the configuration is pretty easy. Place this in your ~/.zshrc:

如果您只想使用 ZSH 来更好地完成,那么配置非常简单。把它放在你的 ~/.zshrc 中:

autoload -U zutil      # [1]
autoload -U compinit   # [2]
autoload -U complist   # [3]
compinit

However, it's worth checking out all the other great features of the ZSH. The above example will give you a pretty plain prompt with good completion. If you don't want to fiddle with configurations, but want to see what ZSH can do for you, Google for "zshrc" and you will get some ready to use configurations to get started.

但是,值得一试 ZSH 的所有其他强大功能。上面的例子会给你一个非常简单的提示,完成度很好。如果你不想摆弄配置,但想看看 ZSH 能为你做什么,谷歌搜索“zshrc”,你会得到一些准备好使用的配置来开始。

回答by Staale

zsh has a console gui configuration thing. You can set it up pretty quickly and easily without having to fiddle with configuration files. I don't think you will need much time to set it up, probably 10 seconds with just using defaults, so go ahead and try it out.

zsh 有一个控制台 gui 配置的东西。您可以快速轻松地设置它,而无需摆弄配置文件。我认为您不需要太多时间来设置它,仅使用默认值可能需要 10 秒,所以请继续尝试。

回答by Agnel Kurian

Staale is talking about a wizard like program (CUI) which autoruns the first time you run zsh. Just answer some questions, view/change the defaults and its configured for you.

Staale 正在谈论类似程序 (CUI) 的向导,它会在您第一次运行 zsh 时自动运行。只需回答一些问题,查看/更改默认值及其为您配置的内容。

IBM developerWorks has great resources on zsh.

IBM developerWorks 拥有关于 zsh 的大量资源。

I have not used very advanced features and so far I have not come across serious differences which should hamper someone coming from bash.

我没有使用过非常高级的功能,到目前为止我还没有遇到严重的差异,这会妨碍来自 bash 的人。

Some examples:

一些例子:

  • !?pattern<Tab> will autocomplete to the last command in history matching pattern. Very useful.

  • You can configure a prompt on the RHS. One use is to keep a fixed width prompt on the left hand side so all commands line up nicely while displaying the pwd (or anything of variable width) as the right hand side prompt.

  • You can redirect input from multiple files (yet to try this): cat < file1 < file2 < file3

  • !?pattern<Tab> 将自动完成历史匹配模式中的最后一个命令。很有用。

  • 您可以在 RHS 上配置提示。一种用途是在左侧保持固定宽度的提示,以便在将 pwd(或任何可变宽度的)显示为右侧提示时,所有命令都可以很好地排列。

  • 您可以重定向来自多个文件的输入(尚未尝试此操作): cat < file1 < file2 < file3