The following code best describes the situation. Why is the last line not outputting the trailing newline char? Each line's output is shown in the comment. I'm using GNU bash, version 4.1.5
echo -n $'a\nb\n' | xxd -p # 610a620a
x=$'a\nb\n' ; echo -n "$x" | xxd -p # 610a620a
echo -ne "a\nb\n" | xxd -p # 610a620a
x="$(echo -ne "a\nb\n")" ; echo -n "$x" | xxd -p # 610a62
tmp=$(somecommand; echo a); tmp=${tmp%a}
tmp=$(somecommand; echo a)
... This has certainly driven the point home... Until I saw the example, my tendancy would still have been to useecho -n a
... but, of course!, there is no need of the-n
, because Command Substitution will remove the introduced trailing newline in any case! ... thanks...