When running the following command, sqlcmd.exe writes "Timeout Expired" to the console but the exit code it returns is 0.
sqlcmd.exe -X1 -b -S "[ValidServer]" -d "[ValidDatabase]" -t 5 -i "C:\test\ScriptThatRunsLongerThan5Seconds.sql" -E
Please notice I'm purposely setting the querytimeout, -t, to 5 seconds. Also, I'm the -b argument, which I thought should return exit code 1 for errors. I've also tried the -r1 argument and the -m-1 argument to no avail.
I've also looked at the Troubleshooting: Timeout Expired article and noticed that it references an "Error: -2". The sqlcmd.exe documentation for all the arguments related to errors seem to only deal with error codes greater than 0, so perhaps "Timeout expired" is not an error? Also, no matter what arguments I've tried I only see "Timeout expired" as the output, no error code at all.
Lastly, here are all the commands I've tried, only edited to protect server and database names:
C:\>sqlcmd.exe -X1 -b -S "[ValidServer]" -d "[ValidDatabase]" -t 5 -i "C:\test\ScriptThatRunsLongerTHan5Seconds.sql" -E
Timeout expired
C:\>sqlcmd.exe -X1 -b -S "[ValidServer]" -d "[ValidDatabase]" -t 5 -i "C:\test\ScriptThatRunsLongerTHan5Seconds.sql" -E -r1
Timeout expired
C:\>sqlcmd.exe -X1 -b -S "[ValidServer]" -d "[ValidDatabase]" -t 5 -i "C:\test\ScriptThatRunsLongerTHan5Seconds.sql" -E -V16
Timeout expired
C:\>sqlcmd.exe -X1 -b -S "[ValidServer]" -d "[ValidDatabase]" -t 5 -i "C:\test\ScriptThatRunsLongerTHan5Seconds.sql" -E -m-1
Timeout expired
C:\>sqlcmd.exe -X1 -S "[ValidServer]" -d "[ValidDatabase]" -t 5 -i "C:\test\ScriptThatRunsLongerTHan5Seconds.sql" -E -m-1
Timeout expired
C:\>sqlcmd.exe -X1 -b -S "[ValidServer]" -d "[ValidDatabase]" -t 5 -i "C:\test\ScriptThatRunsLongerTHan5Seconds.sql" -E -r0
Timeout expired
C:\>sqlcmd.exe -X1 -b -S "[ValidServer]" -d "[ValidDatabase]" -t 5 -i "C:\test\ScriptThatRunsLongerTHan5Seconds.sql" -E -o "sqlError.txt"
C:\>notepad sqlError.txt
#REM only "Timeout expired" was written to sqlError.txt
C:\>del sqlError.txt
C:\>sqlcmd.exe -X1 -S "[ValidServer]" -d "[ValidDatabase]" -t 5 -i "C:\test\ScriptThatRunsLongerTHan5Seconds.sql" -E -m-1
Timeout expired
C:\>sqlcmd.exe -X1 -S "[ValidServer]" -d "[ValidDatabase]" -t 5 -i "C:\test\ScriptThatRunsLongerTHan5Seconds.sql" -E -V-3
Sqlcmd: '-V -3': Severity level has to be a number between 1 and 25.
C:\>sqlcmd.exe -X1 -S "[ValidServer]" -d "[ValidDatabase]" -t 5 -i "C:\test\ScriptThatRunsLongerTHan5Seconds.sql" -E -V1
Timeout expired
C:\>sqlcmd.exe -X1 -S "[ValidServer]" -d "[ValidDatabase]" -t 5 -i "C:\test\ScriptThatRunsLongerTHan5Seconds.sql" -E -r1
Timeout expired
Update: I should note I'm calling this from a C# console application via the Systems.Diagnostics.Process, so I'd like the ExitCode to be 1 if possible. I'm not sure if I can capture ERRORLEVEL.
Further Update: My machine is writing stderr to stdout for some reason, so I'm unable to test the validity of Stefan M's answer. I believe his answer is most likely correct as I've interpreted the documentation for sqlcmd.exe in the same manner. I have another question about why my machine is writing stderr to stdout here.