0

This seems very easy (and it probably is), but I'm having some problems with saving a result of a pipe to a variable.

Let's say this is the output of the pipe:

This
is
the
output
of 
the
pipe

Which look exactly as I want. However, if I try to store the pipe into variable:

var=$(...pipe...)

The output of the echo $var will be:

This is the output of the pipe

Also tried with printf, but it doesn't work either.

Rok Dolinar
  • 976
  • 3
  • 13
  • 29

1 Answers1

2

Your assignment is fine, but you need to quote the variable in echo:

echo "$var"

It is probably best practice to put quotes on the assignment and write var="$(...)", but it's not actually necessary since word splitting does not occur on the RHS of an assignment.

William Pursell
  • 204,365
  • 48
  • 270
  • 300