title: Using bash(cygwin) inside C# program
body: <p>i need to use bash shell "inside" C# program. I want to mimic user typing in interactive mode and running cygwin commands.</p>
  
  <p>i created a process that runs bash and redirect stdin,stout and std error but i can;t get tty to work attached is a sample code that starts bash process and redirect the input/output.</p>
  
  <p>the problem is that i don't have tty device. if i try to run tty command or stty command i receive error response </p>
  
  <pre><code>tty - not a tty 
  stty - Inappropriate ioctl for device
  </code></pre>
  
  <p>i think the this is caused from <code>psi.UseShellExecute = false;</code></p>
  
  <p>i need to run cygwin and disable echo with stty -echo but to do this i need a tty device. how ca i create a cygwin bash shell with tty device and redirect the stdin, out and error ?</p>
  
  <p>1) what am missing ?</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Diagnostics;
  using System.Linq;
  using System.Text;
  using System.Threading;
  
  namespace shartCygwin
  {
      class Program
      {
          private static Queue&lt;string&gt; ResponseQueue = null;
          private static ManualResetEvent ResponseEvent = null;
  
          static void Main(string[] args)
          {
              ResponseQueue = new Queue&lt;string&gt;();
              ResponseEvent = new ManualResetEvent(false);
  
              Process bashProcess = new Process();
  
              bashProcess.StartInfo.FileName = "C:\\cygwin\\bin\\bash.exe"; 
              bashProcess.StartInfo.Arguments = "--login -i ";  
              bashProcess.StartInfo.WorkingDirectory = "C:\\cygwin\\bin";
  
              bashProcess.StartInfo.EnvironmentVariables["CYGWIN"] = "tty";
  
              bashProcess.StartInfo.RedirectStandardError = true;
              bashProcess.StartInfo.RedirectStandardInput = true;
              bashProcess.StartInfo.RedirectStandardOutput = true;
              bashProcess.StartInfo.CreateNoWindow = true;
              bashProcess.StartInfo.UseShellExecute = false;
              bashProcess.StartInfo.ErrorDialog = false;
  
              bashProcess.Start();
  
              DataReceivedEventHandler errorEventHandler = new DataReceivedEventHandler(ErrorDataReceived);
              DataReceivedEventHandler outEventHandler = new DataReceivedEventHandler(OutDataReceived);
              bashProcess.OutputDataReceived += outEventHandler;
              bashProcess.ErrorDataReceived += errorEventHandler;
              bashProcess.BeginErrorReadLine();
              bashProcess.BeginOutputReadLine();
  
              while(true)
              {
                  Thread.Sleep(1000);
              }
          }
  
          static void ErrorDataReceived(object sender, DataReceivedEventArgs dataReceivedEventArgs)
          {
              try
              {
                  lock (ResponseQueue)
                  {
                      Console.WriteLine(dataReceivedEventArgs.Data);
                      ResponseQueue.Enqueue(dataReceivedEventArgs.Data);
                      ResponseEvent.Set();
                  }
              }
              catch (Exception e)
              {
                  Console.WriteLine(e.Data);
              }
          }
  
          static void OutDataReceived(object sender, DataReceivedEventArgs dataReceivedEventArgs)
          {
              try
              {
                  lock (ResponseQueue)
                  {
                      Console.WriteLine(dataReceivedEventArgs.Data);
                      ResponseQueue.Enqueue(dataReceivedEventArgs.Data);
                      ResponseEvent.Set();
                  }
              }
              catch (Exception e)
              {
                  Console.WriteLine(e.Data);
              }
          }
      }
  }
  </code></pre>