4

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();
}
winry
  • 311
  • 2
  • 10

1 Answers1

4

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'";
winry
  • 311
  • 2
  • 10
  • 1
    and `"-l -c 'echo $0; read' 'blah'"` should give the same output. The "blah" string is now the 0th command-line argument. – Eryk Sun Sep 24 '17 at 07:56