MacOS – working with lf x0A in bash terminal

bashmacos

I'm mystified why the variable $lf isn't printing out when using hexdump -C

This is a copy with comments added from a run in macOS 10.10.5 bash terminal.

mac $ lf=$(echo -ne  "\x0a")
# What happened to the lf?
mac $ echo -n ${lf} | hexdump -C
# The lf seems to be working or should it be two blank lines? 
mac $ echo $lf

# one lf as expected.
mac $ echo "" | hexdump -C
00000000  0a                                                |.|
00000001
# only one lf as expected.
mac $ echo | hexdump -C
00000000  0a                                                |.|
00000001
# since echo return an lf, why not use this fact to create lf
mac $ lf=$(echo)
# Why no lf?
mac $ echo -n ${lf} | hexdump -C
mac $ echo $lf

# one lf where I'd expect to see two. 
mac $ echo  ${lf} | hexdump -C
00000000  0a                                                |.|
00000001

# cr seems to be working as expected.

mac $ cr=$(echo -ne  "\x0D")
mac $ echo -n $cr | hexdump -C
00000000  0d                                                |.|
00000001

Best Answer

From man bash:

Command Substitution

[...]

Bash performs the expansion by executing command and replacing the command substitution with the standard output of the command, with any trailing newlines deleted.