linux - Checking whether a shell option is set with [ -o OPTION ], weird behavior when OPTION is on -



linux - Checking whether a shell option is set with [ -o OPTION ], weird behavior when OPTION is on -

i in process of reading this bash guide linux documentation project. on page 81 , 82 there's short illustration script testing whether alternative set:

if [ -o noclobber ] echo "your files protected against accidental overwriting using redirection." fi

i have run weird behavior when trying negate test. getting homecoming value of 0 options turned on [ -o alternative ] , [ ! -o alternative ]. here's example:

$ set -o | grep errex errexit off $ [ -o errexit ]; echo $? 1 $ [ ! -o errexit ]; echo $? 0 $ set -o | grep history history on $ [ -o history ]; echo $? 0 $ [ ! -o history ]; echo $? 0

use [[ ! -o alternative ]] instead. parsing of expressions in [[ ]] more predictable.

the result you're seeing [ because there 2 -o operators in bash's test builtin: unary -o option check if alternative set, , binary test1 -o test2 check if either test true (logical or).

you passing test 3 arguments, !, -o , history. let's see posix says it how parse 3 arguments:

3 arguments: - if $2 binary primary, perform binary test of $1 , $3. - if $1 '!', negate two-argument test of $2 , $3. (...)

-o, indeed binary operator, performs test of $1 , $3, become "is non-empty" check (like in [ ! ] , [ history ]). result hence true.

the sec interpretation expected, it's not used since first interpretation matched.

linux bash gnu

Comments

Popular posts from this blog

assembly - What is the addressing mode for ld, add, and rjmp instructions? -

vowpalwabbit - Interpreting Vowpal Wabbit results: Why are some lines appended by "h"? -

Is there a way to convert an HTML page styled with Bootstrap CSS into email-compatible html? -