Looks to me like the command test "$?" != "0" ends up setting $? to 1. The value $? gets used in the arguments to test. test sets $? to a non-zero value because "0" is lexically equal to "0". The "!=" makes test return non-zero.