Guerrilla
Guerrilla

Reputation: 14906

Bash command not executing via .net core Process

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?

Upvotes: 3

Views: 2877

Answers (2)

Gopi
Gopi

Reputation: 1331

Use double quote instead of single quote while setting Arguments field.

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

Upvotes: 0

Guerrilla
Guerrilla

Reputation: 14906

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

Upvotes: 1

Related Questions