The Bash interpreter itself has options.

For example, those mentioned on lines 22-23 of Bash's man page:

All of the single-character shell options documented in the description of the set builtin command can be used as options when the shell is invoked. In addition, bash interprets the following options when it is invoked:
-c ...
-i ...
-l ...
-r ...

I've used a few search patterns in Bash's man page like:

  • /^\s*set
  • /list

Is it possible to print a list of these settings that are applied to the current shell?

5 Answers 5

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):


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 -p) 
#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).


trap 'echo this is my trap' 0

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.

  • thanks, your code works nicely. so to get a full list of all options, you would have to use both set +o and shopt ? (as to shoptable being a word, settable is used in the man page, so shoptable works for me) Jun 17, 2015 at 7:27
  • @user4668401 - I think that will do it.
    – mikeserv
    Jun 17, 2015 at 7:37
  • 1
    TL;DR, but +1000 for the 3 lines solution to change an option and put it back 'as it was'. Thats exactly what one needs when creating scripts or copy/pastable instructions.
    – Balmipour
    Aug 10, 2017 at 10:09
  • 3
    @mikeserv Seems that -p option needs to be added when saving state to include shoptions, i.e. state=$(set +o;shopt -p)
    – nkatsar
    Feb 9, 2018 at 16:58

From the man page:

...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.

  • that worked, thanks. Do you you know if this lists the options that are settable by both set and shopt? Jun 17, 2015 at 7:13
  • @user4668401 No, set is far more extensive: see the man page entry.
    – jasonwryan
    Jun 17, 2015 at 7:15
  • And to check if a single option is on: shopt <option>
    – kos
    Jun 17, 2015 at 7:29

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`


$ 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"

To list all options:

set -o

To inspect a specific option:

set -o | grep <OPTION>

Take option verbose as an example:

$ set -o | grep verbose
verbose         off

To inspect a specific option:

if shopt -q <OPTION>
then echo '<OPTION> is set'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.