bash 与 zsh 相比的独特功能

bash 与 zsh 相比的独特功能

我已经使用 zsh 很长时间了(之前是 tcsh,之前是 csh)。我对它很满意,但我想知道 bash 中是否有 zsh 所没有的引人注目的功能。反过来,zsh 中是否有 bash 所没有的功能。我目前的感觉是 bash 更好:

  • 如果您已经熟悉它并且不想学习新的语法。
  • 它默认存在于大多数 *nix 机器上,而 zsh 可能需要额外安装。

并不是想在这里引发宗教战争,这就是为什么我只是在寻找只存在于其中一个外壳中的特征。

答案1

zsh 适用于 vulcans。;-)

认真地说:bash 4.0 具有一些以前只能在 zsh 中找到的功能,例如 ** 通配符:

% ls /usr/src/**/Makefile

相当于:

% find /usr/src -name "Makefile"

但显然更加强大。

根据我的经验,bash 的可编程补全性能比 zsh 更好一些,至少在某些情况下是这样(例如,为 aptitude 补全 debian 包)。

bash 必须Alt + .插入!$

zsh has expansion of all variables, so you can use e.g.

% rm !$<Tab>

for this. zsh can also expand a command in backtics, so

% cat `echo blubb | sed 's/u/a/'`<Tab>

yields

% cat blabb

I find it very useful to expand rm *, as you can see what would be removed and can maybe remove one or two files from the commmand to prevent them from being deleted.

Also nice: using the output from commands for other commands that do not read from stdin but expect a filename:

% diff <(sort foo) <(sort bar)

From what I read bash-completion also supports completing remote filenames over ssh if you use ssh-agent, which used to be a good reason to switch to zsh.

Aliases in zsh can be defined to work on the whole line instead of just at the beginning:

% alias -g ...="../.."
% cd ...

答案2

我想指出的是,bash 在 FreeBSD、OpenBSD 或 NetBSD 上未默认安装,在 Solaris 10 上也未默认安装(OpenSolaris 将其作为默认安装),上次我使用 AIX 和/或 HP-UX 服务器时,它也没有默认安装。

此外,在 OpenSolaris 上 /bin/sh 不是 bash。它是 ksh。作为软件移植者,我遇到的最大问题是人们认为 /bin/sh 是 bash,并且它会接受 bash 扩展语法。虽然大多数 Linux 发行版似乎都是这样,但其他地方却不是这样,这真的很烦人。

答案3

虽然我是 bash 用户,但我发现 zsh 的一个功能非常酷:RPS1。

记住:

  • PS1:左对齐提示
  • RPS1:右对齐提示

样本:

当使用类似

PS1=’%B(%h) %m%#%b ‘
RPS1=’(%20<…<%~)'

您会在左侧看到提示符,而当前目录会推到右侧。当前行太长时,它甚至会消失!这是因为 zsh 足够聪明,会为 RPS1 赋予较低的优先级。

您可以在以下位置看到此示例的屏幕截图https://i.stack.imgur.com/W3qDx.jpg

答案4

我不知道 zsh 不支持哪些 bash 功能。zsh 的设计目标似乎包括支持 bash 添加的任何功能。

我仍然使用 bash 而不是 zsh。我很少遇到 zsh 支持而 bash 不支持的引人注目的功能。多年来,zsh 偶尔会出现问题,或者在某些系统上不存在,这使得转换不值得。

我最终可以在每个 Unix 系统上使用相同的 shell,为了那些我永远不会使用的功能而破坏这一点是不值得的。

zsh 中存在而 bash 中不存在的功能似乎大多很可爱,但并不是日常使用中重要的功能。

相关内容