If ! shopt –oq posix; then

I've searched all the pages about this, but I haven't found a clear explanation for it. Can somebody help me please?


1 Answer 1


Assuming If is actually if, the line introduces an if statement that runs the command ! shopt -oq posix as a test.

The shopt utility in the bash shell is a utility used for setting certain shell options. This utility can also be used to test whether a particular shell option is set or not. This is what the utility is used for in the if statement in the question.

The -o option makes it expect that the option given is one of the options that are valid for set -o (see help set in bash), and the -q option makes the utility quiet, i.e. it will only produce an exit status but no output. The exit status of shopt will be used by the if statement to determine whether to execute the body of the if statement or not.

The test checks whether the posix shell option is set in the bash shell, and if it isn't (the interpretation of the result of the shopt call is inverted by the preceding !), the body of the if statement would be executed.

See also help shopt in an interactive bash session.

The shopt utilitiy is not a standard utility, but a built-in utility provided by the bash shell specifically.

  • 1
    Please keep in mind that a bash with set -o posix active is still not POSIX/UNIX compliant. You need at least to set shopt -s xpg_echo in addition.
    – schily
    Commented Jun 18, 2020 at 10:46

You must log in to answer this question.

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