Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

# <some command>
# echo $?

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

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

share|improve this question
up vote 10 down vote accepted

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.

share|improve this answer
To be clear, you should use the single quote ' instead of the double quote ". – Michael Hampton Jan 16 '13 at 7:40
Thanks guys . all of you ! – user154645 Jan 16 '13 at 9:13

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.