Here-String and Newline Convention Complexity

By Xah Lee. Date: .

when you have a here-string (aka heredoc), there's a complexity.

The problem is, if your source code file is using a Microsoft Windows newline convention, then your code won't work on files that has unix newline convention.

Here's a example in PowerShell.

# 2021-12-08
# testing replace a multiline string with herestring, where the destination file contain either unix or windows line ending.

$unix = "eol_lf.txt"
$win = "eol_crlf.txt"

Set-Content $unix -value "a`nb`n" -NoNewLine
Set-Content $win -value "a`r`nb`r`n" -NoNewLine

$repStr = @'

Set-Content ($win + "~") -value (gc $win -raw).Replace($repStr, "") -NoNewLine

Set-Content ($unix + "~") -value (gc $unix -raw).Replace($repStr, "") -NoNewLine

The problem probably happens in bash, php, perl, too.