Search code examples
c#linuxshellportabilityeol

Create linux shell script files in C# (which does not need dos2unix )


I use C# to create shell scripts to automate my tasks in Linux. In order to do this, I use below structure:

List<string> batchFileLines = new List<string>();
batchFileLines.Add("shell command 1");
batchFileLines.Add("shell command 2");
System.IO.File.WriteAllLines(shellBatchFileName, batchFileLines.ToArray());

However when I move my file to linux due to EOL difference in windows and linux (which a fixed suggested here for linux), EOLs in shell file need to be corrected with dos2unix command. I want to know how can I manipulate my file in C# so it is not required to execute dos2unix.

What would be the way to replace new lines with linux format with minimum effort?


Solution

  • Using this answer, I should change

    System.IO.File.WriteAllLines(shellBatchFileName, batchFileLines.ToArray());
    

    to

    System.IO.File.WriteAllText(shellBatchFileName, string.Join("\n", batchFileLines.ToArray()));
    

    The output file will not require dos2unix anymore.