Search code examples
unixtcleol

Unix line endings under Windows


I need to generate a file with Unix line endings (\n) in my Windows environment. The following script still generates the default Windows line endings, despite the fconfigure command. How can I fix this?

set fid [open "myfile.txt" "WRONLY CREAT TRUNC"]
fconfigure $fid -eofchar \n
puts $fid "hello"
close $fid

Solution

  • You need to configure -translation lf on that channel. (binary would also work, but that shorthand also changes other features of the channel, such as the encoding and EOF marker.)

    fconfigure $fid -translation lf
    

    Don't set -eofchar to newline; that's very unlikely to be what you want! (It's also not nearly so important on output; we don't automatically write the EOF marker when you close the file.)