0
var1=$(printf "\n\x0A\n")

var2=$(printf "\na\nb\nc")

Using Android Terminal, the output of echo -e "$var1" (same for echo and printf) iss nothing, not even the 3 new lines. But echo -e "$var2" or echo "$var2" or printf "$var2" output:

a
b
c

How come $var2's output includes the whitespace character but not $var1?

(\n and \x0A have the exact same behaviour.)

muru
  • 69,900
  • 13
  • 192
  • 292
neverMind9
  • 1,680
  • 2
  • 18
  • 38
  • 3
    Possible duplicate of [Why does shell Command Substitution gobble up a trailing newline char?](https://unix.stackexchange.com/questions/17747/why-does-shell-command-substitution-gobble-up-a-trailing-newline-char) (note that it's not just one trailing newline - all trailing newline characters are removed) – muru May 10 '19 at 09:25
  • 1
    Also see: https://unix.stackexchange.com/questions/383217/shell-keep-trailing-newlines-n-in-command-substitution – muru May 10 '19 at 09:26
  • 1
    Replace teh double quots with single quotes, and put everything after the `=` sign between double quotes – wurtel May 10 '19 at 10:53
  • 1
    Note also that `echo` inserts a newline after the output, but `printf` does not. – Seamus May 10 '19 at 13:20

1 Answers1

0

Why would you print a result of a printf?

var1="\n\x0A\n"
var2="\na\nb\nc"

printf $var1
printf $var2

that should work.

*nix system - LF, 0x0A (dec 10) \n as line ending

windows OS - CR LF, 0x0D 0x0A (dec: 13 10) \r \n as line ending.

Michael D.
  • 2,820
  • 16
  • 24