# When exporting text, how can I set the line separator?

I want to export a string for a bash script. The terminal is naturally very picky when it comes down to the "\n\r" vs "\n", Windows vs. Linux, new line issue.

How can I teach Mathematica to use "\n" rather than "\n\r" as the line separator when exporting text (in Windows)?

I found a couple of posts regards the same issue, but they were concerned with importing files. For example, here. None of the suggested solutions worked for my case.

I played with CharacterEncoding and "LineSeparators" but nothing worked.

EDIT: Great thanks to Mark Adler for giving me the right clue. His suggested solution WriteString[f, "\n"] did not work because "\n" is interpreted by Mathematica as string not as end-of-line.

However, his suggested DOSTextFormat -> False option also works as option for Export[].

I got the desired result using Export[filename,script,"Text",DOSTextFormat -> False].

-

You can use WriteString[] which does not append a new line, and then write the new line character yourself. E.g. WriteString[f, "\n"].

Also you can try DOSTextFormat -> False as an OpenWrite[] option. I don't have a Windows system to try it out on.

-

Great thanks to Mark Adler for giving me the right clue. His suggested solution WriteString[f, "\n"] did not work because "\n" is interpreted by Mathematica as string not as end-of-line.

However, his suggested DOSTextFormat -> False option also works as option for Export[].

I got the desired result using Export[filename,script,"Text",DOSTextFormat -> False].

-

This did not work for me because I wanted to create a Windows CSV file from a Macintosh. I tried DOSTextFormat->True but it did nothing. I found that you have the most control using the "Table" format because you can set the field and line separators. For CSV windows files:

Export[fileToWrite, avgdata, "Table", "FieldSeparators" -> ",", "LineSeparators" -> "\r\n"]


For CSV Mac/Unix files:

Export[fileToWrite, avgdata, "Table", "FieldSeparators" -> ",", "LineSeparators" -> "\n"]

-