What is "( set -o posix ; set ) | less " doing?

set shows all shell variables (exported or not). In Bash, set -o posix sets the shell in POSIX compatibility mode. (I don't know if other shells have similar syntax for the similar feature, but I'll assume Bash here.)

The difference in this case is that usually Bash's set shows also shell functions, but in POSIX mode set only shows variables, and changes the output format slightly:

  1. When the set builtin is invoked without options, it does not display shell function names and definitions.
  2. When the set builtin is invoked without options, it displays variable values without quotes, unless they contain shell metacharacters, even if the result contains nonprinting characters.

In Bash, there's additionally the declare builtin that can be used to show all the otherwise hidden or Bash-specific flags of variables: declare -p xx shows variable xx in a format that Bash can take as input. declare -p shows all variables and declare -f can be used to show functions.


There's two things going on basically: the set commands are being called in subshell to avoid messing up your current shell options, and that subshell is being set to POSIX mode before listing all the variables. To quote the manual:

posix Change the behavior of bash where the default operation differs from the POSIX standard to match the standard (posix mode). See SEE ALSO below for a reference to a document that details how posix mode affects bash's behavior.

In short, bash in POSIX mode will behave closer to what sh does.

As for option-less set, it's also in the manual and it actually states the reason why POSIX mode is necessary:

set [+abefhkmnptuvxBCEHPT] [+o option-name] [arg ...] Without options, the name and value of each shell variable are displayed in a format that can be reused as input for setting or resetting the currently-set variables. . .In posix mode, only shell variables are listed.

...as opposed to variables and function definitions, which is what's done whenbash is in regular mode.

See this for more info about POSIX mode.