How can I list bash'es options for the current shell?

printf %s\\n "$-"

Will list the single letter options in a single string.

That parameter can also be used like:

set -f -- ${-:+"-$-"}
echo *don\'t* *glob* *this*
set +f "$@"

To first disable shell -filename expansion while simultaneously saving a value for $- - if any - in $1. Next, no globs occur, and last +filename expansion is once again enabled, and possibly also disabled.

For example, if -filename expansion was already disabled when the value for $- was first saved, then its saved value would be (at least):

f

And so when set is run again, it works out to:

set +f -f

Which just puts you right back where you started.

set +o

Will list all settable shell options (see Jason's answer for the shoptable - is that a word? - options) in a form that is safe for shell reentry. In that way, you can also do:

state=$(set +o)
set -some -crazy -options
eval "$state"

To save, change, and restore the shell options' state respectively.

To handle shoptions and settable options in one go:

state=$(set +o;shopt)
#do what you want with options here
eval "$state"

You can also call set without any arguments to add a list of all of the shell's currently set variables - also quoted for reentry to the shell. And you can - in bash - additionally add the command typeset -fp to also include all currently declared shell functions. You can lump it all together and eval when ready. You can even call alias without arguments for more of the same. That... might cover it, though. I guess there is "$@" - which you'd have to put in a bash array first, I suppose, before doing set.

Nope, there's also trap. This one's a little funny. Usually:

trap 'echo this is my trap' 0
(echo this is my subshell; trap)

...will just print this is my subshell because the subshell is a new process and gets its own set of traps - and so doesn't inherit any traps but those which its parent has explicitly ignored - (like trap '' INT).

However:

trap 'echo this is my trap' 0
save_traps=$(trap)

trap behaves specially when it is the first and only command run in a command substitution subshell in that it will reproduce a list of the parent shell's currently set traps in a format which is quoted for safe reentry to the shell. And so you can do the save_traps, then set without arguments - and all of the rest already mentioned - to pretty much get a lock on all shell state. You might want to explicitly add export -p and readonly -p to restore original shell var attributes, though.

Anyway, that's enough.


From the man page:

shopt
...With no options, or with the -p option, a list of all settable options is displayed, with an indication of whether or not each is set.
shopt | grep "on$"

will print a list of all the enabled options.


Options?: There are three groups of options in bash.

  1. The options read or set by the command set.
    ( use this command for the manual: LESS=+/'set \[--abefhkmnptuvxBCEHPT\]' man bash )
    All the one-letter options: abefhkmnptuvxBCEHPT

    Also the options (which have no single letter equivalent):

    set -o emacs
    set -o history
    set -o ignoreeof
    set -o interactive-comments
    set -o pipefail
    set -o posix
    set -o vi
    
  2. The options read or set by the command shopt.
    ( read the manual with this command: LESS=+/'shopt \[-pqsu\]' man bash' )
    The list is quite long but easy to print, use:

    $ shopt -p
    

    or the similar (but not equal):

    $ shopt
    
  3. The options that could be set on INVOCATION:
    ( command for the manual: LESS=+/'^INVOCATION' man bash )

    All set options:

    bash -abefhkmnptuvxBCEHPT
    

    All shopt options

    bash -O option
    

    Some options that are only meaningful when INVOKING bash:

    bash -lirsDc
    

    And, also (long options):

    bash --debugger --help --init-file file --rcfile file --login \
         --noediting --noprofile --norc --posix --restricted \
         --verbose --version
    

All set one letter options and -ri are printed with $-.

-i      Interactive  
Only read with `$-` (if it contains an `i`).
-r      Restricted
Also read with `shopt -p restricted_shell

For example:

$ echo 'echo $0 $-' | bash -abefhikmprtuvxBCEHPT -lri

Calling bash as a login shell is only reported with shopt:

-l      Login
read with `shopt -p login_shell`

Thus:

$ echo 'shopt -p login_shell' | bash -abefhikmprtuvxBCEHPT -lri
shopt -s login_shell

The option -n can not be tested by any code, as it means: do not execute any code.


Printing options

The best tool to list the options of the running shell, either the ones set with set or the ones set with shopt is the same shopt. To list set options:

shopt -o

Which acts exactly as set -o. Use shopt -po to get the same result as set +o.

To list shopt options:

shopt -p

Of which you can choose to print either the ones set shopt -ps or unset shopt -pu

To get all options. As much as it is possible as many long options and some ( -sDc ) on INVOCATION can not be printed:

$ echo $-; shopt -p ; shopt -po

To store them in a variable:

$ storeoptions="$(echo "set -$-"; shopt -p ; shopt -po)"

And the variable could be used to set back all the options:

$ eval "$storeoptions"

Tags:

Bash

Shopt