Super User is a question and answer site for computer enthusiasts and power users. 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

ssh -i private_linux_key user@machine "exit 1" || echo %errorlevel%

This always prints 0. How can I get actual error code returned by ssh?

|| somehow detects that there was an error.

share|improve this question
Well ssh exited cleanly, so the return value is 0. – Oliver Salzburg Apr 22 '13 at 15:57
I understand this. But what is the proper way to get exit code returned from ssh session? – Roman Apr 23 '13 at 7:17
You could append ;echo $? to your command, but that won't set the errorlevel variable :\ – Oliver Salzburg Apr 23 '13 at 11:46
up vote 2 down vote accepted

I've found a solution, thanks to following question:

setlocal enabledelayedexpansion
ssh -i private_linux_key user@machine "exit 1" || echo !errorlevel!

Or even simplier

ssh -i private_linux_key user@machine "exit 1"

The latter is possible only when there are no more additional script lines after exit, of course

To read more about enabledelayedexpansion and windows/unix shells differences:

share|improve this answer

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.