Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

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

I have the following case :- I write a batch file bbb in Windows 2003 and put a return value = 3 by exit /b 3 then I execute this batch file from Unix by this command :- ssh -l admin host 'cmd /c start bbb' but when I print the return value I get ( 0 ) not ( 3 ). I print this value by echo $?. Now how can I get a return value "exit code" from Windows batch?

share|improve this question
up vote 3 down vote accepted

You can't use the exact code for bash and batch at the same time.

My guess would be the script follows a conditional, something like:

  exit /b 3
  exit 3

That's all pseudocode, I'm not sure what works for both systems.

Edit: Or you could go the easier route and write up two entirely different script files; job.bat and

share|improve this answer

I was intrigued by this so I did a bit of research. I think that using the start command is causing you problems - start causes the command to run in another shell. When you exit /b 3 the return value gets passed to the shell that is run by cmd /c and when it exits you just get a 0 returned to your ssh and therefore $? is 0.

I created a simple batch file in c:\temp\b.bat

@echo off
exit /b 3 

running the batch file like this

ssh -l admin host 'cmd /c c:/temp/b.bat'

returned 3 to $? as required.

Note: the use of / instead of \ in the command above.

My test environment was copssh 3.1.4 on Windows Vista but I would expect it to work the same in your 2k3 environment.

share|improve this answer

I think what is happening is your command is returning the exit code from ssh itself, and not the command ssh is executing. I could be wrong, but I don't think there's a way to make ssh pass through the return code of anything it executes remotely.

You probably need to make your "bbb" bash file echo some text if something went wrong, and then collect and check for that text.

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.