Your method of checking the return value of the command is the problem. From a shell:
$ true | echo $?
0
$ false | echo $?
0
As you’re creating a pipeline, the echo $?
part would show the exit code of the previous command. And since the last element of each pipeline you ran succeeds, you’re always seeing a zero.
Try using ; echo $?
or && echo connected
instead.