如何检查是否已设置 bash shell 属性?

Tim*_*Tim 4 bash

set -o posix 将设置 bash shell 的 POSIX 属性。

我们如何检查属性是否已设置?

小智 8

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

勾选一个选项。

要为 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)