要启用一个选项,我们可以使用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
.
Sté*_*las 21
只需使用:
if [[ -o extended_glob ]]; then
echo it is set
fi
Run Code Online (Sandbox Code Playgroud)
这也适用于bash
,但仅适用于由 设置的选项set -o
,不适用于由 设置的选项shopt
。zsh
只有一组选项可以使用setopt
或进行设置set -o
。
就像使用bash
(或任何 POSIX shell)一样,您也可以执行set -o
或set +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)
归档时间: |
|
查看次数: |
13799 次 |
最近记录: |