5

I wonder if there is a difference between

if [ $x -eq $y -o $x -eq 1 ]
if [ $x -eq $y ] || [ $x -eq 1 ]

Both seem to work. Thanks

2
  • That looks like || vs -o, not && vs -a. Although, the relationship is probably the same. Nov 13, 2016 at 20:03
  • 1
    @Zacharee1 edited the title. OP clearly just wants to know what's yhe difference between single test with minus options and two test commands Nov 13, 2016 at 20:16

2 Answers 2

10

TL;DR: they're not the same and the purpose is slightly different, although they aim at same desired effect.

The difference is in who does the logic checks. As you may know, the square brackets are synonymous with test command. So, with -o you see single test command, and the two conditions are evaluated together, then returned exit status to bash.

With && you have two test statements, each returning separately, but then bash evaluates if they are both true. The last one is recommended nowadays , since test is a bit dated and has issues (and new version of test [[ is often recommended, see this), plus it is far more readable since the && or || are used in many other languages including C and Java.

So in first case, with [ you evaluate two expressions, with && you evaluate exit statuses of two [ comands.

Additional info:

As pointed out by @steeldriver in the comments, there is indeed built-in command test, which is bash's internal implementation of /usr/bin/test. Regardless of this fact, the way built-in test and /usr/bin/test work syntactically is exactly the same.

Keep in mind, however, that if you're using some other shell than bash or Ubuntu's default shell dash, then you will likely be using /usr/bin/test . Korn shell, ksh for instance, doesn't have test built-in.

Keep in mind also the fact that && and test are not equivalent:

  • test is supposed to evaluate statements and return exit status like any other command (see POSIX specifications for test). It has a whole list of things you can evaluate - existence of files, filetypes, integer and string comparison.
  • Logic operators such as && and || are used by shells to evaluate exit statuses of separate commands just like ; does, which is one of the reasons in bash man page these operators are under Lists section. In addition, they cannot be a stand-alone command , like test - they are standard shell list operators . So, unlike test you cannot use && to check if file exists; you can only know if command to the left exited successfully or with failure, and run or not run second command accordingly.
6
  • Isn't test a shell builtin in bash as well? Trying type [ seems to indicate so. Isn't the more significant difference that || and && are short circuit operators? Nov 13, 2016 at 21:14
  • @steeldriver short-circuit evaluation isn't bash-specific, first of all. Any implementation of logic operators does that, ever since C programming language had them. As for [ , there is indeed bash built-in for that, but depending on your system and shell you use, there might not be such built-in. Additionally, it's not the same as && , i.e. they're not equivalent. && is bash control operator, and can serve as list separator. [ is only for expressions. In other words, what's being evaluated here is also different. I'll add that to my answer Nov 13, 2016 at 21:22
  • Huh? where did I imply that short-circuit evaluation is bash-specific? or that [ is "the same as" &&? Nov 13, 2016 at 21:26
  • @steeldriver So you said Isn't the more significant difference that || and && are short circuit operators .Technically, yes. The more significant difference is the purpose of these two things. One is supposed to be standalone command evaluating expressions, the other is for evaluating exit statuses of commands. As for bash-specific part, I guess I misread your comment. By the way, correct terminology isn't "short-circuit operators" ,but "control operators". Also, test seems not to be required by POSIX shell , because they require test to be standalone, so that's another difference Nov 13, 2016 at 21:43
  • 2
    No problem, nice answer btw - your English is better than many native speakers! Nov 13, 2016 at 22:01
1

The only difference I can see would be that the first of your command would work as a single process ([, AKA test)., while the second one may produce two processes if the first command returns non-zero.

No fundamental difference otherwise.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.