I have a bash terminal open. I want to find out whether the option extglob is enabled or disabled in this session, before I change its value. How can I do this?
4 Answers
Just run:
$ shopt extglob
It will return the current status:
$ shopt extglob
extglob on
$ shopt -u extglob
$ shopt extglob
extglob off
To show all options, just run:
$ shopt
-
This one can also be used for scripting, its return status also indicate option set or unset, but the message will mess standard out.– cuonglmCommented Jan 14, 2016 at 17:10
Use shopt -q:
shopt -q extglob && echo enable || echo disable
-q
option make shopt
discard output, and return status to indicate that options set or unset.
Note that shopt
only reports options which can appear in BASHOPTS
variable, those options are not valid to set
builtin command.
To check for options which are valid to set
, or can be appear in SHELLOPTS
, use shopt -qo
:
$ bash --posix -c 'shopt -qo posix && echo enable || echo disable'
enable
-
2
-
1Because I don't want the output to mess my standard out :)– cuonglmCommented Jan 14, 2016 at 17:09
-
What do you mean? As far as I can tell, the OP wants to check whether the option is set or not. They mention that this is in a terminal, so I doubt it's part of a script. And, anyway, what's the difference? Both
shopt extglob
and your approach will write to stdout. Are you thinking ofshopt -q extglob && shopt -u extglob || shopt -s extglob
?– terdon ♦Commented Jan 14, 2016 at 17:11 -
@terdon: Well, my approach only write to stdout if I want. The general approach is
shopt -q extglob && : Code when enable || : Code when disable
.– cuonglmCommented Jan 14, 2016 at 17:16 -
4I can imagine writing a script that would attempt to preserve the user's existing setting, but might have a critical bit of code that will break if it's not set a particular way. This suggests a method to silently determine the current state, save it, and restore it ASAP after performing the crucial operations that require potentially-different state. Commented Jan 14, 2016 at 19:07
There are two lists of options in bash. One for shopt
and one for set
.
The option extglob
belongs to the shopt
list.
Its value may be printed by using either shopt extglob
or shopt -p extglob
.
An option like nounset
belongs to the set
list.
Its value may be printed by using shopt -op nounset
or shopt -o nounset
.
Check one option.
To print an specific option (without changing it) for shopt, use shopt -p name
:
$ shopt -p xpg_echo
shopt -u xpg_echo
And for set
, use: shopt -po name
(yes, you may use shopt -op
for set
list).
$ shopt -po xtrace
set +o xtrace
List options.
To list all options from shopt, use shopt
(or reusable shopt -p
).
Also shopt -s
or shopt -u
could be used.
The way to list all options to set
is with set -o
(related: set +o
).
Or: shopt -o
is equivalent to set -o
and shopt -op
is to set +o
.
Manual
From LESS=+/'^ *shopt \[' man bash
:
With no options, or with the -p option, a list of all settable options is displayed, If either -s or -u is used with no optname arguments, the display is limited to those options which are set or unset, respectively.
From LESS=+/'^ *set \[' man bash
:
If -o is supplied with no option-name, the values of the current options are printed. If +o is supplied with no option-name, a series of set commands to recreate the current option settings is displayed on the standard output.
Examples
$ 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
And
$ 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
It is worth mentioning about shopt -op
which actually lists set
options:
$ 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
Adding to the answer given above, for set
flags,
The current set of flags may be found in $-.
(source: bash help)
if [[ ${-%x*} != $- ]] ; then echo "Debug (xtrace) is on"; fi
-
The question is about
extglob
which in the context of the answer you linked to "belongs to theshopt
list". The linked answer is kind enough to cover the "set
list", but what counts is it covers the "shop
list" and therefore can be used to know aboutextglob
. So it answers the question. Your answer alone does not answer the question becauseextglob
is not a "set
flag" and its state does not affect$-
. If the question was about Bash options in general then your answer would fit. The question is specific, so IMO your "answer" should be a comment or an edit to the linked answer. Commented Dec 30, 2021 at 9:31 -
@KamilMaciorowski Thank you for that detailed critique, but ... did you read my answer? Line 1: Adding to the answer (link given). Perhaps it would have been better if I had edited the answer. I'm not here for upvotes. I'm here for utility. Comments are not useful for providing answers, only remarking on them.– OtheusCommented Feb 23, 2022 at 11:41
-
My point is your answer does not answer the question. Each answer should be standalone. "Adding to another answer" is allowed if what you add also answers the question. Possibly if what you add expands a fragment (of the other answer) that answers the question. Your answer expands a fragment that does not answer the question. The fragment is there as something extra next to a fragment that does. I still think your "answer" does not fit the Q&A scheme, it should be a comment or an edit to the linked answer. But I'm not on a crusade; I have stated my opinion and now I'm going to pass. Commented Feb 23, 2022 at 12:32
shopt -s extglob
or, if you want it off, runshopt -u extglob
. It doesn't make any difference whatsoever if it was on originally or not.