2

I'm using the man page for "test". I understood everything except for the following 2 lines. Can someone give me an example of how to actually use this because thus far I've been using 1 dimensional tests (i.e. is x = y kind of thing in my tests), so comparing expressions doesn't make too much sense in my head. I think if someone can show me an example or use case I'd get it.

   EXPRESSION1 -a EXPRESSION2
          both EXPRESSION1 and EXPRESSION2 are true

   EXPRESSION1 -o EXPRESSION2
          either EXPRESSION1 or EXPRESSION2 is true
john smith
  • 676
  • 4
  • 13
  • 24
  • 2
    You tagged `bash` and used the phrase "man page for `test`", so *maybe* you are not aware of the following: `test` is a builtin in Bash and `help test` in Bash describes it, but `man test` describes a standalone executable. The builtin and the executable may differ in what they support. Depending on what syntax you use and where (`test`, `/usr/bin/test`, `find … -exec test …`, `sudo test …`, …), you use one or the other. I don't think the answer to your question depends on what `test` you use, but in general it's good to know `man test` does not describe the builtin. – Kamil Maciorowski Dec 11 '22 at 19:55

1 Answers1

5

-a and -o join two expressions, i.e. complete expressions which could be used independently with test. Thus

test "$x" = foo -o "$x" = bar

succeeds if

test "$x" = foo

or

test "$x" = bar

would succeed, i.e. "$x" is either foo or bar.

This can be ambiguous, so -a and -o are deprecated, and the shell’s operators should be used instead:

test "$x" = foo || test "$x" = bar

More commonly, this would be written

[ "$x" = foo ] || [ "$x" = bar ]

See Unexpected result for evaluation of logical or in POSIX sh conditional for an example of -o being used in a confusing manner.

Stephen Kitt
  • 411,918
  • 54
  • 1,065
  • 1,164
  • So if I look at the last line, i still find this confusing. The last line reads: "is x equal to foo, if this is not true, then is x equal to bar?" . So, if I assume x=abc, then another way to read the last line is "x is not foo, and it's also not bar". Thus the overall result is false, Is this correct? – john smith Dec 11 '22 at 19:55
  • 1
    If `x` is `abc`, then yes, the overall result is false. “`x` is not `foo`, and it’s also not `bar`” isn’t equivalent, it’s the negation (see De Morgan’s laws). – Stephen Kitt Dec 11 '22 at 20:00