This script will exit with test result which is not desired from callers perspective. How it should be implemented instead?

test -n "" && echo "test passed"
  • 1
    if the caller cares about the exit code, then i cant understand how anything else would be desirable. if i called a program which only tested if a null-string was not-null and it returned true, it would upset me.
    – mikeserv
    Commented Dec 7, 2015 at 11:39
  • 1
    No, it exits with the result of echo if the test was successful and test if it wasn't. That said, what is your desired behavior? What should it exit with?
    – terdon
    Commented Dec 7, 2015 at 13:06

1 Answer 1


Just add an explicit:

exit 0

at the end of your script if you don't want to report any failure.

You can also do:

#! /bin/sh -

cmd || ret=$? # we care about the failure of cmd

test -n "" && echo test # we don't care about the failure of test or echo

exit "$ret"

For your particular example, you could rewrite it:

test -z "" || echo test passed

(that would still report an error if echo failed, but you may want to report that one as that would indicate something bad)


test -n "" && echo test passed || : ignore

The : command always returns with a success exit status.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .