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?
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityAssuming 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.
bash
with set -o posix
active is still not POSIX/UNIX
compliant. You need at least to set shopt -s xpg_echo
in addition.
POSIX
, it is rather a non-portablebashism
.