zsh:如何检查选项是否已启用

Séb*_*ien 55 zsh

要启用一个选项,我们可以使用setopt. 例如:

setopt extended_glob
Run Code Online (Sandbox Code Playgroud)

我们如何检查当前是否启用了一个选项?

cuo*_*glm 67

在 中zsh,您可以使用setopt来显示已启用的选项和unsetopt未启用的选项:

$ setopt
autocd
histignorealldups
interactive
monitor
sharehistory
shinstdin
zle

$ unsetopt
noaliases
allexport
noalwayslastprompt
alwaystoend
noappendhistory
autocd
autocontinue
noautolist
noautomenu
autonamedirs
.....
Run Code Online (Sandbox Code Playgroud)

在 中bash,您可以使用shopt -p.

  • 您应该使用 `set -o` 来获取完整列表。 (9认同)

Sté*_*las 21

只需使用:

if [[ -o extended_glob ]]; then
  echo it is set
fi
Run Code Online (Sandbox Code Playgroud)

这也适用于bash,但仅适用于由 设置的选项set -o,不适用于由 设置的选项shoptzsh只有一组选项可以使用setopt或进行设置set -o

就像使用bash(或任何 POSIX shell)一样,您也可以执行set -oset +o查看当前选项设置。


Gil*_*il' 16

zsh/parameter模块是默认发行版的一部分,提供了一个关联数组options,指示哪些选项已打开。

if [[ $options[extended_glob] = on ]]; then …
Run Code Online (Sandbox Code Playgroud)

对于具有单字母别名的选项(不是 的情况extended_glob),您还可以检查$-.

请注意,测试启用了哪些选项很少有用。如果您需要启用或禁用一段代码中的选项,请将该代码放入一个函数中并设置该local_options选项。您可以调用emulate内置函数将选项重置为默认状态。

my_function () {
  setopt extended_glob local_options
}
another_function () {
  emulate -L zsh
  setopt extended_glob
}
Run Code Online (Sandbox Code Playgroud)