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

bec*_*cko 11 bash terminal

我打开了一个 bash 终端。在更改其值之前,我想知道在此会话中是启用还是禁用选项 extglob。我怎样才能做到这一点?

Jef*_*ler 18

赶紧跑:

$ shopt extglob
Run Code Online (Sandbox Code Playgroud)

它将返回当前状态:

$ shopt extglob 
extglob         on
$ shopt -u extglob 
$ shopt extglob 
extglob         off
Run Code Online (Sandbox Code Playgroud)

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

$ shopt
Run Code Online (Sandbox Code Playgroud)


cuo*_*glm 13

使用shopt -q

shopt -q extglob && echo enable || echo disable
Run Code Online (Sandbox Code Playgroud)

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

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

要检查对 有效set或可以出现在 中的选项SHELLOPTS,请使用shopt -qo

$ bash --posix -c 'shopt -qo posix && echo enable || echo disable'
enable
Run Code Online (Sandbox Code Playgroud)

  • 我可以想象编写一个脚本来尝试保留用户的现有设置,但如果没有以特定方式设置,可能会有一些关键的代码会中断。这建议了一种方法,可以在执行需要潜在不同状态的关键操作后,以静默方式确定当前状态、保存并尽快恢复它。 (4认同)
  • 为什么不只是`shopt extglob`? (2认同)

小智 8

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

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

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

勾选一个选项。

要为 shopt 打印特定选项(不更改它),请使用shopt -p name

$ shopt -p xpg_echo
shopt -u xpg_echo
Run Code Online (Sandbox Code Playgroud)

而对于set,使用:shopt -po name(是的,你可以使用shopt -opset列表)。

$  shopt -po xtrace
set +o xtrace
Run Code Online (Sandbox Code Playgroud)

列出选项。

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

列出所有选项的方法set是使用set -o(related: set +o)。
或:shopt -o相当于set -o并且shopt -opset +o

手动的

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

不带选项或带 -p 选项时,将显示所有可设置选项的列表。如果使用 -s 或 -u 且不带 optname 参数,则显示分别限于已设置或未设置的选项。

来自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
Run Code Online (Sandbox Code Playgroud)

$  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
Run Code Online (Sandbox Code Playgroud)

值得一提的是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
Run Code Online (Sandbox Code Playgroud)