Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash command not executing via .net core Process

Tags:

c#

.net-core

I have wrote simple command to do a basic echo on Linux.

This is command I want to do:

/bin/bash -c 'echo hello'

This is app I am running:

using System;
using System.Diagnostics;

namespace ProcessTest
{
    class Program
    {
        static void Main(string[] args)
        {

            var startInfo = new ProcessStartInfo
            {
                FileName = @"/bin/bash",
                Arguments = @"-c 'echo hello'",
                RedirectStandardOutput = true,
                UseShellExecute = false
            };

            using (var process = new Process { StartInfo = startInfo })
            {
                process.Start();
                string result = process.StandardOutput.ReadToEnd();
                process.WaitForExit();

                Console.WriteLine(result);

                Console.Read();
            }
        }
    }
}

Instead of outputting "hello" it outputs this:

hello': -c: line 0: unexpected EOF while looking for matching `''
hello': -c: line 1: syntax error: unexpected end of file

Why is it not working?

like image 289
Guerrilla Avatar asked Mar 05 '26 22:03

Guerrilla


1 Answers

Using double quotes works. For some reason it is just single quotes that cause this issue.

like image 196
Guerrilla Avatar answered Mar 07 '26 18:03

Guerrilla



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!