update attempt to clarify more, using an example, does the following shell commands:
SHELLVARIABLE="1st line, 2nd line, 3rd line, " printf '%s' "$SHELLVARIABLE"
generate this output:
1st line,<newline>2nd line,<newline>3rd line,<newline>
<newline> being the character
Original question formulation
What is the correct (POSIX-confirm) way to store a newline character
0x0a (aka known by its commen c style escap
\n) into a shell variable.
I want to make sure that doing the following:
is, not merely working by chance but instead is indeed the correct way.
Since POSIX's printf is perfectly capable of producing a newline character (i.e.
printf '\n', or
printf '%b' '\0012') I first attempted a more explicit (? or correct?) form:
though a tempting approach, does not work. As the according to the standard, command substitution (i.e. via
` `) shall remove
sequences of one or more newlines at the end of the substitution.
Note: Asking to store a single trailing newline into a shell variable is only to abstract the more general use case (i indeed seek answer to) that is how to store a string into POSIX shell variable which ends with the newline character.