3

When I execute a command as below it gives me an output as expected but when I run the same command on remote server it always gives me echo $? as 0.

locally
# <some command>
# echo $?

Remotely
# ssh server " <some command >; echo $? "

Now in case of remote execution I always get 0 as echo $? output.

0

1 Answer 1

10

The $? is evaluated before the command is sent to the remote machine. Effectively, you are sending the command echo 0. Try

ssh server ' <some command >; echo $? '

As Michael Hampton points out, single quotes prevent evaluating variables etc. before sending them to the remote command, while double quotes allow that.

1
  • 3
    To be clear, you should use the single quote ' instead of the double quote ". Jan 16, 2013 at 7:40

You must log in to answer this question.

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