title: How to execute a shell command across platforms in .NET Core?
body: <p>In my .NET Core Console app, I receive multiple commands in form of an array of string, and would like to execute them as console command (and showing their output in my own app if possible but not hard requirement).</p>
  
  <p>At first, I tried to parse each command to separate their name and arguments and put them in <code>ProcessStartInfo</code>. However, some command does not work (even simple commands like <code>echo "Hello"</code>).</p>
  
  <p>Now I switched to call Powershell instead like this:</p>
  
  <pre><code>    static IEnumerable&lt;ProcessStartInfo&gt; ParseCommands(string[] args)
      {
          return args
              .Skip(1)
              .Select(q =&gt; new ProcessStartInfo()
              {
                  FileName = "powershell",
                  Arguments = q,
                  RedirectStandardOutput = true,
                  RedirectStandardError = true,
                  UseShellExecute = false,
              }).ToList();
      }
  
      static void RunCommand(ProcessStartInfo processInfo)
      {
          Console.WriteLine($"{processInfo.Arguments}");
  
          var process = new Process()
          {
              StartInfo = processInfo,
          };
          process.Start();
  
          while (!process.StandardOutput.EndOfStream)
          {
              Console.WriteLine(process.StandardOutput.ReadLine());
          }
  
          process.WaitForExit();
      }
  </code></pre>
  
  <p>The problem is I don't think this one can run on Linux or MacOS. Is there any "standard" way to tell my app to "run this as if it's a console command"?</p>
  
  <hr>
  
  <p>This is my current code by using the Platform to determine the console command,  feel free to tell me if there is a better way:</p>
  
  <pre><code>    static IEnumerable&lt;ProcessStartInfo&gt; ParseCommands(string[] args)
      {
          var argsPrepend = "";
          var shellName = "/bin/bash";
          if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
          {
              shellName = "cmd";
              argsPrepend = "/c ";
          }
  
          return args
              .Skip(1)
              .Select(q =&gt; new ProcessStartInfo()
              {
                  FileName = shellName,
                  Arguments = argsPrepend + q,
                  RedirectStandardOutput = true,
                  RedirectStandardError = true,
                  UseShellExecute = false,
              }).ToList();
      }
  
      static void RunCommand(ProcessStartInfo processInfo)
      {
          Console.WriteLine($"{processInfo.Arguments.Substring(processInfo.FileName == "cmd" ? 3 : 0)}");
  
          var process = new Process()
          {
              StartInfo = processInfo,
          };
          process.Start();
  
          while (!process.StandardOutput.EndOfStream)
          {
              Console.WriteLine(process.StandardOutput.ReadLine());
          }
  
          process.WaitForExit();
      }
  </code></pre>