3

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?

7
  • Dunno if this is of any help for you, but it works for me! (I pasted your code into MonoDevelop and executed it on my Ubuntu 18.04 system - did you meant to execute it on Windows?) Commented Sep 18, 2018 at 13:22
  • No, I want to execute it on Ubuntu 18.04. I am using .net core 2.1. What version of .net are you using? Commented Sep 18, 2018 at 13:24
  • Are the single quote characters exactly the same as each other (neither are backticks, for example)? Commented Sep 18, 2018 at 13:26
  • @Guerrilla: I am sure, this is the source of the problem - it uses Mono 4.6.2 here. So maybe .net core makes s. th. with the tick characters. Commented Sep 18, 2018 at 13:28
  • both single quotes are definitely the same. I wonder if it could be an encoding issue perhaps? Commented Sep 18, 2018 at 13:37

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

What is the purpose of this comment? Stackoverflow has a timer for when you can self mark complete. As the self appointed SO police I assume you already know this?
sorry I didn't know ... my apologizes
0

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

1 Comment

please note that "Try this" is not an answer, please elaborate and add some description for your answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.