Server Fault is a question and answer site for system and network administrators. 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

This simple command fails in bash:

echo "print 'tsql'" | sqlcmd -E

it works correctly in a regular old command prompt.

It used to work with previous versions of cygwin. I've also tried inserting various flavors of unix2dos in there, and I've looked at the output with "od -c". So I'm quite sure it's not just a LF vs. CR/LF problem.

Looking at this thread:

leads me to believe something got broken in cygwin, but I've been unable to chase down any more leads.

Does anyone have any clues, other than generating a temp file, and then using

sqlcmd -E -i tempfile

which does work, but it's ugly.

share|improve this question
try swapping quotes: echo 'print "tsql"' | sqlcmd -E – jftuga May 9 '11 at 15:11
1) it's not a quoting problem, 2) your solution isn't valid TSQL, even if it was a quoting problem – Eric H. May 9 '11 at 18:18

I am using MSYS2 (x86-64, installed with the 20150916 and updated using pacman to the latest default packages as of two days ago) but I ended up encountering the same error (which makes sense as MSYS2 is a cygwin derivative). I never had this with MSYS, but since MSYS is kind of dead and I wanted more modern tools I am switching to MSYS2 and porting my scripts. I found a workaround that is working for me at the moment:

$ echo hi | cmd //c "cat | sqlcmd"

I assume it works because we’re asking cmd to set up the pipe between the MSYS2 process (cat) and the Windows process rather than letting MSYS2/bash do it. Though I also suspect that SQLCMD is doing some evil nasty trickery from Windows programming in the ’90s which it shouldn’t be doing in the first place making it a special case which perhaps deserves a workaround be developed in cygwin/MSYS2.

Note that with this method passing arguments to sqlcmd becomes really difficult, especially if you want to pass a literal ".

Full output from me demonstrating the error, its solution, and showing where my sqlcmd is coming from:

ohnob@WIN-F5A6PNAUAJ2  ~
$ echo hi |sqlcmd -S localhost\\SQLEXPRESS12
Sqlcmd: Error: Internal error at ReadTextLine (Reason: Unspecified error).

ohnob@WIN-F5A6PNAUAJ2  ~
$ echo hi | cmd //c "cat | sqlcmd -S localhost\\SQLEXPRESS12"
Msg 2812, Level 16, State 62, Server WIN-F5A6PNAUAJ2\SQLEXPRESS12, Line 1
Could not find stored procedure 'hi'.

ohnob@WIN-F5A6PNAUAJ2  ~
$ which sqlcmd
/c/Program Files/Microsoft SQL Server/Client SDK/ODBC/110/Tools/Binn/sqlcmd

ohnob@WIN-F5A6PNAUAJ2  ~
$ WHERE cat | head -n 1
share|improve this answer
I needed to supply utf-8 data to sqlcmd. I used 'cmd //U //c ' to enable utf support across the pipe as well as solving the ReadTextLine issue. – codeDr Dec 1 '15 at 20:20

I had similar problems working with SQLCMD in Bash and as of now this question is ranked third when searching for sqlcmd cygwin on Google. I too thought that it was due to line endings because of the error,

Sqlcmd: Error: Internal error at ReadTextLine (Reason: Unspecified error).

I tried various trickery such as process substitution but that didn't work. The best that I could come up with was using

sqlcmd -Q "print 'tsql'"
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.