72

To enable an option, we can use setopt. e.g.:

setopt extended_glob

How can we check if an option is currently enabled ?

5 Answers 5

86

In zsh, you can use setopt to show options enabled and unsetopt to show which are not enabled:

$ setopt
autocd
histignorealldups
interactive
monitor
sharehistory
shinstdin
zle

$ unsetopt
noaliases
allexport
noalwayslastprompt
alwaystoend
noappendhistory
autocd
autocontinue
noautolist
noautomenu
autonamedirs
.....

In bash, you can use shopt -p.

3
  • 1
    It should be noted that setopt only prints the options not enabled by default for that emulation mode.
    – llua
    Mar 28, 2014 at 9:55
  • 13
    You should use set -o for FULL list. Nov 1, 2017 at 14:41
  • set -o|sort for zsh is good.
    – Timo
    Mar 20, 2022 at 13:24
26

Just use:

if [[ -o extended_glob ]]; then
  echo it is set
fi

That also works in bash, but only for the options set by set -o, not those set by shopt. zsh has only one set of options which can be set with either setopt or set -o.

Just like with bash (or any POSIX shell), you can also do set -o or set +o to see the current option settings.

1
  • What is small o flag -o? I read in man test ` -O FILE FILE exists and is owned by the effective user ID` which is an upper O. Wait, in man bash I read -o optname True if the shell option optname is enabled. See the list of options under the description of the -o option to the set builtin below. which should be it!
    – Timo
    Mar 20, 2022 at 13:30
20

The zsh/parameter module, which is part of the default distribution, provides an associative array options that indicates which options are on.

if [[ $options[extended_glob] = on ]]; then …

For options that have a single-letter alias (which is not the case of extended_glob), you can also check $-.

Note that it's rarely useful to test which options are enabled. If you need to enable or disable an option in a piece of code, put that code in a function and set the local_options option. You can call the emulate builtin to reset the options to a default state.

my_function () {
  setopt extended_glob local_options
}
another_function () {
  emulate -L zsh
  setopt extended_glob
}
2
  • Other than greater readability here, and greater brevity with the other one, is there any advantage one way or another between this and if [[ -o extended_glob ]]; then...?
    – iconoclast
    Mar 18, 2022 at 0:00
  • @iconoclast Not that I can think of. Mar 18, 2022 at 9:07
1

If it's only to check on terminal, I find the below helpful.

setopt | grep extended
0
() {[[ -v argv[1] && -v options[$1] ]] && echo "option <$1> is $options[$1]" || echo "no such option <$1>"  } <optname>

With <optname> from https://zsh.sourceforge.io/Doc/Release/Options.html

Examples:

() {[[ -v argv[1] && -v options[$1] ]] && echo "option <$1> is $options[$1]" || echo "no such option <$1>"  } LOCAL_TRAPS

option <LOCAL_TRAPS> is off

() {[[ -v argv[1] && -v options[$1] ]] && echo "option <$1> is $options[$1]" || echo "no such option <$1>"  } localtraps
option <localtraps> is off

() {[[ -v argv[1] && -v options[$1] ]] && echo "option <$1> is $options[$1]" || echo "no such option <$1>"  } localtrap 
no such option <localtrap>

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .