Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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.

share|improve this question
add comment

1 Answer

up vote 9 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
3  
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
add comment

Your Answer

 
discard

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.