4

I just wanted to understand the meaning of the following statement and whether they seem correct.

test -x /usr/bin/find || exit 0 
Command 1
Command 2
Command 3

The output of test -x /usr/bin/find is always 0. That means the exit 0 command will be executed , meaning Command 1, 2, 3 will never be executed. Am I right here?

7

test -x /usr/bin/find (or [ -x /usr/bin/find ]) does not output anything. The test will be true if /usr/bin/find is an existing executable file, and false if the pathname does not exist, or if it's not executable.

If test exits successfully (with a zero exits status, signifying "no error"), the shell will execute the rest of the commands. If it exits with a failure (a non-zero exit status, signifying "some error"), exit 0 will terminate the current shell, preventing the rest of the commands from running.

It would arguably be better to use exit 1 or just exit in place of exit 0 when find can't be found in /usr/bin though. Using exit 0 masks the exit status of test (which would be non-zero), and prevents the caller of this script from being notified of the failure of finding find at the given location.

Related to the fact that an exit status of zero evaluates to "true" when tested as a boolean in the shell:

Related to using || and && in general:

0
4

No, they will always be executed. In man bash it says (it works like that in each POSIX shell):

   An OR list has the form

          command1 || command2

   command2  is executed if, and only if, command1 returns a non-zero exit
   status.

Since test -x /usr/bin/find returns 0 command2 (exit 0 in this case) is not executed and script continues execution.

3
  • Can you add a similar description for AND also? Jan 10 at 10:11
  • How would a command like (a && b) || c will get executed? Jan 10 at 10:12
  • 1
    @DawsonSmith it is exactly the same: your confusion stems from the fact that in the shell, a 0 exit status means success and not failure as is the case in many other contexts.
    – terdon
    Jan 10 at 10:15

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.