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.

Khaled
  • 36,533
  • 8
  • 72
  • 99
user154645
  • 33
  • 4

1 Answers1

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.

Sven
  • 98,649
  • 14
  • 180
  • 226