In consul-template I want to pass a ENV var with with new lines that will be expanded so "hello\nworld" is shown as:
hello
world
command:
VARIABLE="hell\nworld" consul-template -template "in.tpl:out.txt" -once && cat out.txt
template file: {{ env "VARIABLE" }}
however I am getting
hello\nworld
If I debug the template I am showed the \n has been escaped to \\n:
{{ env "VARIABLE" | spew_dump }}
"hello\\nworld"
In
consul-templateGiven the new-line is as a C-Escape
\nin a golang double quoted string literal, there is thereplaceAllconsul template function1 readily available (similar toenv) to replace\nwith U+000A New Line (Nl) line feed (lf) , end of line (eol) , LF.This is the format as shown by the
spew_dumpin question.Note that this replaces
\nonly, not\rnor the other escape sequences.In Shell (earlier)
There is no
\nC-Escape in double-quoted strings like"hello\nworld", butprintf(1)has it - or - if your shell supports them,$''strings (bash, zsh/z-shell).Compare with How can I have a newline in a string in sh?.
Examples:
Example #1:
$''quoted stringExample #2:
printf(1)Mind that command substitution (
$(...)) may remove a trailing newline.