如何从 C# 执行 cygwin 命令并保持打开状态?

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

我正在尝试通过 bash 从 C# 运行 一个 cygwin 命令,并尝试使用读取命令保持结果打开。我的代码似乎正在打开 bash 然后立即关闭。我做错了什么?

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();
}

刚刚找到解决方案。用单引号将整个命令括起来使其有效。所以

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

应替换为以下内容:

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