0

I am facing this situation I do not understand.

I have a file containing three lines. When I just cat it, it returns the following:

line1
line2
line3

However, when I assign the cat return to a variable like so:

tmp=$(cat myfile )

And echo its content:

echo $tmp

It gives the following:

line1line2line3

Why does it behave like this ?

Cyrus
  • 84,225
  • 14
  • 89
  • 153
Itération 122442
  • 2,644
  • 2
  • 27
  • 73
  • 3
    Use `echo "$tmp"` to preserve newlines, spaces, tabs ... – Cyrus Jun 04 '20 at 08:44
  • It does. But my situation is a bit more complexe. The cat is sent through a pipe. `VAR=$(cat ../tmp/offset_tmp | dosomething)`. How should I proceed in this case ? @Socowi – Itération 122442 Jun 04 '20 at 08:50
  • Proceed like Cyrus suggested. The pipe is irrelevant. Your variable contains the correct value, but that value gets mangled if you write `$tmp` instead of `"$tmp"`. – Socowi Jun 04 '20 at 08:51
  • You are safe if you always use double quotes when accessing the contents of variables. – Cyrus Jun 04 '20 at 09:07

0 Answers0