无法获取手册的系统变量

发布于 2024-07-17 13:53:43 字数 410 浏览 3 评论 0原文

我在 .zshrc 中有以下系统变量,

manuals='/usr/share/man/man<1-9>'

我运行失败,

zgrep -c compinit $manuals/zsh*

我得到

zsh: no matches found: /usr/share/man/man<1-9>/zsh*

命令应该与以下有效命令相同

zgrep -c compinit /usr/share/man/man<1-9>/zsh*

如何在 Zsh 中使用系统变量运行上述命令?

I have the following system variable in .zshrc

manuals='/usr/share/man/man<1-9>'

I run unsuccessfully

zgrep -c compinit $manuals/zsh*

I get

zsh: no matches found: /usr/share/man/man<1-9>/zsh*

The command should be the same as the following command which works

zgrep -c compinit /usr/share/man/man<1-9>/zsh*

How can you run the above command with a system variable in Zsh?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忱杏 2024-07-24 13:53:43

尝试:

gt; manuals=/usr/share/man/man<0-9>
gt; zgrep -c compinit ${~manuals}/zsh*

“~”告诉 zsh 在使用变量时执行 <0-9> 的扩展。 zsh 参考卡告诉您如何执行此操作以及更多操作。

Try:

gt; manuals=/usr/share/man/man<0-9>
gt; zgrep -c compinit ${~manuals}/zsh*

The '~' tells zsh to perform expansion of the <0-9> when using the variable. The zsh reference card tells you how to do this and more.

蓝天白云 2024-07-24 13:53:43

根据我的调查,zsh 似乎在 <> 替换之前 $ 替换执行。 这意味着当您使用 $ 变体时,它首先尝试 <> 替换(那里什么也没有),然后尝试 $ 替换(有效),剩下的就是包含 <> 字符的字符串。

当您不使用 $manuals 时,它首先尝试 <> 替换并且有效。 这是一个秩序问题。 下面的最终版本展示了如何推迟扩展,以便它们同时发生:

这些可以在这里看到:

> manuals='/usr/share/man/man<1-9>'

> echo $manuals
  /usr/share/man/man<1-9>

> echo /usr/share/man/man<1-9>
  /usr/share/man/man1 /usr/share/man/man2 /usr/share/man/man3
  /usr/share/man/man4 /usr/share/man/man5 /usr/share/man/man6
  /usr/share/man/man7 /usr/share/man/man8

> echo $~manuals
  /usr/share/man/man1 /usr/share/man/man2 /usr/share/man/man3
  /usr/share/man/man4 /usr/share/man/man5 /usr/share/man/man6
  /usr/share/man/man7 /usr/share/man/man8

From my investigations, it looks like zsh performs <> substitution before $ substitution. That means when you use the $ variant, it first tries <> substitution (nothing there) then $ substitution (which works), and you're left with the string containing the <> characters.

When you don't use $manuals, it first tries <> substitution and it works. It's a matter of order. The final version below shows how to defer expansion so they happen at the same time:

These can be seen here:

> manuals='/usr/share/man/man<1-9>'

> echo $manuals
  /usr/share/man/man<1-9>

> echo /usr/share/man/man<1-9>
  /usr/share/man/man1 /usr/share/man/man2 /usr/share/man/man3
  /usr/share/man/man4 /usr/share/man/man5 /usr/share/man/man6
  /usr/share/man/man7 /usr/share/man/man8

> echo $~manuals
  /usr/share/man/man1 /usr/share/man/man2 /usr/share/man/man3
  /usr/share/man/man4 /usr/share/man/man5 /usr/share/man/man6
  /usr/share/man/man7 /usr/share/man/man8
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文