如何知道当前bash会话中是否启用了extglob?


9

我有一个bash终端打开。我想在更改此值之前先确定此会话中的extglob选项是启用还是禁用。我怎样才能做到这一点?


2
请注意,除非您有好奇心,否则无需检查。如果要打开,请运行shopt -s extglob,如果要关闭,请运行shopt -u extglob。不论是否原始,它都没有任何区别。
terdon

Answers:


15

赶紧跑:

$ shopt extglob

它将返回当前状态:

$ shopt extglob 
extglob         on
$ shopt -u extglob 
$ shopt extglob 
extglob         off

要显示所有选项,只需运行:

$ shopt

该选项也可以用于脚本编写,其返回状态还可以指示选项已设置或未设置,但是该消息将使标准混乱。
cuonglm '16

11

使用shopt -q

shopt -q extglob && echo enable || echo disable

-qoption shopt放弃输出,并返回状态以指示选项已设置或未设置。

请注意,shopt仅报告可以在BASHOPTS变量中显示的选项,这些选项对set内置命令无效。

要检查对set或有效的选项SHELLOPTS,请使用shopt -qo

$ bash --posix -c 'shopt -qo posix && echo enable || echo disable'
enable

2
为什么不只是shopt extglob呢?
terdon

1
因为我不想让输出弄乱我的标准:)
cuonglm '16

你什么意思?据我所知,OP希望检查是否设置了该选项。他们提到这是在终端中,所以我怀疑它是否是脚本的一部分。而且,有什么区别?两者shopt extglob和您的方法都将写入stdout。你在想shopt -q extglob && shopt -u extglob || shopt -s extglob什么?
terdon

@terdon:好吧,我的方法只在需要时才写到stdout 。一般的方法是shopt -q extglob && : Code when enable || : Code when disable
cuonglm '16

3
我可以想象编写一个脚本来尝试保留用户的现有设置,但是如果没有设置特定的方式,可能会有一些关键代码会中断。这建议了一种方法,可以在执行需要潜在不同状态的关键操作后,静默确定当前状态,保存并尽快恢复它。
蒙迪·哈德

5

bash中有两个选项列表。一为shoptset

该选项extglob属于shopt列表。
可以使用shopt extglob或来打印其值shopt -p extglob

像这样的选项nounset属于该set列表。
其值可以使用shopt -op nounset或打印shopt -o nounset

检查一个选项。

要为购物者打印特定选项(无需更改),请使用shopt -p name

$ shopt -p xpg_echo
shopt -u xpg_echo

对于set,请使用:(shopt -po name是的,您可以将其shopt -op用于set列表)。

$  shopt -po xtrace
set +o xtrace

列出选项。

要列出购物商店中的所有选项,请使用shopt(或可重用shopt -p)。
此外shopt -s或者shopt -u也可以使用。

列出所有选项的方法setset -o(相关:)set +o
或者:shopt -o相当于set -oshopt -opset +o

手册

来自LESS=+/'^ *shopt \[' man bash

如果不使用任何选项,或者使用-p选项,则显示所有可设置选项的列表。如果不使用optname参数使用-s或-u,则显示分别限于设置或未设置的那些选项。

来自LESS=+/'^ *set \[' man bash

如果提供的-o没有选项名,则打印当前选项的值。如果提供的+ o没有选项名称,则在标准输出上显示一系列用于重新创建当前选项设置的set命令。

例子

$ set -o
allexport       off
braceexpand     on
emacs           on
errexit         off
errtrace        off
functrace       off
hashall         on
histexpand      on
history         on
ignoreeof       off
interactive-comments    on
keyword         off
monitor         on
noclobber       off
noexec          off
noglob          off
nolog           off
notify          off
nounset         off
onecmd          off
physical        off
pipefail        off
posix           off
privileged      off
verbose         off
vi              off
xtrace          off

$  shopt -sp
shopt -s checkwinsize
shopt -s cmdhist
shopt -s expand_aliases
shopt -s extglob
shopt -s extquote
shopt -s force_fignore
shopt -s histappend
shopt -s histverify
shopt -s interactive_comments
shopt -s progcomp
shopt -s promptvars
shopt -s sourcepath

值得一提的是shopt -op哪些实际上列出了set选项:

$ shopt -op
set +o allexport
set -o braceexpand
set -o emacs
set +o errexit
set +o errtrace
set +o functrace
set -o hashall
set -o histexpand
set -o history
set +o ignoreeof
set -o interactive-comments
set +o keyword
set -o monitor
set +o noclobber
set +o noexec
set +o noglob
set +o nolog
set +o notify
set +o nounset
set +o onecmd
set +o physical
set +o pipefail
set +o posix
set +o privileged
set +o verbose
set +o vi
set +o xtrace
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.