0

There are many answers to the question "how do I store and print newlines in a bash variable". Briefly, the answer is usually that a variable containing newlines needs to be quoted when it is evaluated.

e.g.

foo="1
2"
echo "$foo"

outputs

1
2

and

echo $foo

outputs

1 2

At this point, one might think that the lines of the variable could be joined into a single line by assigning them to another variable without quotes.

e.g.

bar=$foo

However

echo "$bar"

outputs

1
2

The answer to this question may already have been asked. But, the abundance of answers to similar questions may be crowding-out existing answers to the question I'm interested in. So, it needs to be asked: why does unquoted variable evaluation discard newlines while unquoted variable evaluation in an assignment preserve newlines?

Answers without reference to documentation won't really be useful since the behavior is already clear. Language-lawyers would likely provide the best answers.

Lotney
  • 301
  • 1
  • 6
  • 1
    It's due to [word splitting](https://www.gnu.org/software/bash/manual/bash.html#Word-Splitting). – Benjamin W. May 09 '20 at 00:49
  • I'm not a lawyer sorry, but path/file name expansion and word splitting does not happen during the variable assignment. That's why quoting is optional on the assignment. – Jetchisel May 09 '20 at 00:50
  • 1
    @Jetchisel Unless there is whitespace on the RHS. – Benjamin W. May 09 '20 at 00:51
  • 1
    The canonical Q&A is https://stackoverflow.com/q/29378566/3266847 - does that not answer your question? Lots of references into the POSIX spec, too. – Benjamin W. May 09 '20 at 00:51
  • @BenjaminW. Yes, the links you provided should be sufficient with all the details. – Jetchisel May 09 '20 at 00:52
  • I must be missing something but, AFAICT, all of the references given in comments only address why `echo $foo` doesn't print newlines. They don't indicate why `bar=$foo` preserves newlines. Even if I am missing the reason in those references, it would be useful to have a specific and concise explanation of that phenomenon here. – Lotney May 09 '20 at 01:06
  • @BenjaminW., @Jetchisel, @shelter, can anyone explain the difference between `echo $foo` and `bar=$foo`. The references given don't address that specific point. In fact, they seem to imply that the newlines should be lost in both cases. – Lotney May 09 '20 at 01:18
  • [Bash manual](https://www.gnu.org/software/bash/manual/bash.html#Shell-Parameters), about assignments: "Word splitting is not performed" – Benjamin W. May 09 '20 at 03:04
  • See also [this Q&A](https://stackoverflow.com/q/3958681/3266847). – Benjamin W. May 09 '20 at 03:09
  • Also [this question](https://stackoverflow.com/questions/38173313/why-does-field-splitting-not-occur-after-parameter-expansion-in-an-assignment-st), and [this unix&linux question](https://unix.stackexchange.com/questions/68694/when-is-double-quoting-necessary), which also covers the (few) other cases where double-quoting variable references is not needed. – Gordon Davisson May 09 '20 at 07:41

0 Answers0