0

Why does the expression

[ 1 -eq 1 -a 1 -eq 2 ] 

returns exit code 1

while [ true -a false ] doesent? The latter always oputputs exitcode 0 no matter how I combine true and false. I thought true and false are commands wich return exit code 0 or 1. If that is so why isnt the expression outputing exit code 1? I would like to know how this exactly works. Thanks for your help. I am new to linux so sorry if its too basic of a question. Thanks.

  • Because both true and false are non-empty strings??? – oguz ismail May 25 '20 at 10:39
  • By the way, note that `-a` is marked "obsolescent" in [the POSIX standard for `test`](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/test.html), and shouldn't be used in new code. Combine multiple separate `test` calls with `&&` instead, as in `[ 1 -eq 1 ] && [ 1 -eq 2 ]` – Charles Duffy May 25 '20 at 12:51

1 Answers1

1

The [ ... ] is a command, which is called with the arguments, that you give it (that is also why the spaces are required). If you now give it an argument, that one is evaluated: For example if you give it the string literal [ test ] it determines that it is a nonempty string and returns 0. If you give it an empty string [ ] it will return 1. True and false in this case are also treated as literal strings, so they both are nonempty and return 0.

For more information read this question

hippodev
  • 91
  • 4
  • Duplicative questions should be closed, not answered. See the "Answer Well-Asked Questions" section of [How to Answer](https://stackoverflow.com/help/how-to-answer), and particularly the bullet point therein regarding questions that "have been asked and answered many times before". – Charles Duffy May 25 '20 at 12:47