bash lore: command substitution and final new lines

Be wary that command substitution will remove all final new lines.

Command substitution are $(command ..) construct or older `command ..` one that will be substitued by the standard output of the given command.

But that's incorrect: a proper definition would have been: It will be substitued by the standard output minus any final new lines. For example:

$ value_w_final_dot="$(echo -en "hello\n\n\n.")"
$ value_wo_final_dot="$(echo -en "hello\n\n\n")"
$ echo "My first value: <<$value_w_final_dot>>"
My first value: <<hello


.>>
$ echo "My second value: <<$value_wo_final_dot>>"
My second value: <<hello>>

When you are bitten by these types of "features", don't you value more the python motto "explicit is better than implicit" ?.

This chopping occurs at command substitution time. You can definitively store ending new lines in a bash variable, in command parameters, and send them of course over a pipe, just avoid command substitution:

$ value_wo_final_dot="hello"$'\n\n\n'
$ echo "My corrected value: <<$value_wo_final_dot>>"
My first value: <<hello


>>
$ /bin/echo "As system process argument: " "$value_wo_final_dot"
As system process argument: hello



$