Search code examples
c#cygwinwindows-console

How to execute a cygwin command from C# and leave it open?


I'm trying to run a cygwin command via bash from C# and try to keep the results open with the read command. My code appears to be opening the bash then immediately closing. What am I doing wrong?

static void Main(string[] args)
{
    Process bashProcess = new Process();
    bashProcess.StartInfo.FileName = @"C:\cygwin64\bin\bash.exe";
    bashProcess.StartInfo.Arguments = "-l -c echo blah; read";
    bashProcess.StartInfo.UseShellExecute = true;
    bashProcess.Start();


    bashProcess.WaitForExit();
    System.Console.WriteLine("Exit Code: {0}", bashProcess.ExitCode);

    System.Console.WriteLine("Press enter to exit...");
    System.Console.ReadLine();
}

Solution

  • Just found a solution. Enclosing the entire command with single quotes makes it work. So

    bashProcess.StartInfo.Arguments = "-l -c echo blah; read";
    

    should be replaced with the following:

    bashProcess.StartInfo.Arguments = "-l -c 'echo blah; read'";