How to check shell options?

I want to check what options are enabled for my current shell. For example, I want to know whether I was invoked with -x or -e or any number of other options.

I source a shell script with shebang line #!/bin/bash -x and later attempt to turn off 'x' with a set +x at the end of the script. Now I want to check whether my set +x line succeeded.

Attempted solution

I've discovered printenv and set as ways to inspect some info about the current shell, but neither of them seem to contain a variable that stores x e or the like.

2

1 Answer

It's in the $- variable.

See the documentation for the set command -- scroll down to the end and you'll see "The current set of options may be found in $-."

A quick demo

$ bash
$ echo $-
himBH
$ set -f; set +H
$ echo $-
fhimB
$ [[ $- == *i* ]] && echo This is an interactive shell
This is an interactive shell

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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

You Might Also Like