newline in bash variable

8

My script contains something like this:

ifc=$(ifconfig)

With this, the ifc variable contains the output of the command ifconfig but without newlines. So, when I print it with echo $ifc I get only one line.

How can I include the newlines of subcommands?

Ethan Leroy

Posted 2010-10-01T08:49:58.163

Reputation: 496

Answers

12

The correct way to print is

echo "$ifc"

livibetter

Posted 2010-10-01T08:49:58.163

Reputation: 1 497

Works! Why, though? – Armand – 2013-04-08T11:22:23.570

see Word Splitting in bash(1), you need quoting to preserve the spaces, tabs, or newlines. – livibetter – 2013-04-08T14:30:52.703