bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Handling files with CRLF line ending


From: Yair Lenga
Subject: Handling files with CRLF line ending
Date: Sat, 3 Dec 2022 05:40:02 -0500

Hi,

I was recently asked to deploy a bash/python based solution to windows
(WSL2).  The solution was developed on Linux. Bash is being used as a glue
to connect the python based data processing (pipes, files, ...). Everything
works as expected with a small BUT: files created by python can not be read
by bash `read` and `readarray`.

The root cause is the CRLF line ending ("\r\n") - python on windows uses
the platform CRLF line ending (as opposed to LF line ending for Linux).

The short term (Dirty, but very quick) solution was to add dos2unix pipe
when reading the files. However, I'm wonder about a better solution: add
"autocrlf" to basic input/output.

Basically, new option "autocrlf" (set -o autocrlf), which will allow bash
scripts (under Unix and Windows) to work with text files that have CRLF
line ending. The goal should be to minimize the number of changes that are
needed.

Possible better alternative will be use environment variable
("BASH_AUTOCRLF" ? ) that will perform the same, with the advantage that it
will be inherit by sub processes, making it possible to activate the
behavior, without having to modify the actual code. Huge plus in certain
situations.

Specifically:
* For "read": remove CR before line ending if autocrlf is on.
* For "readarray": the '-t' option should also strip CRLF line ending if
autocrlf is on.
* No impact on other commands, in particular echo/printf. It's impossible
to know if a specific printf/echo should produce CRLF, as it is unknown if
the program that will read the data is capable of handing CRLF line ending.

Feedback/comments.

Yair


reply via email to

[Prev in Thread] Current Thread [Next in Thread]