0
ifconfigvar=$(ifconfig eth0) && echo $ifconfigvar

You can see that output of the above command has no formatting:

eth0 Link encap:Ethernet HWaddr 00:1f:16:ef:5b:c0 UP BROADCAST MULTICAST MTU:1500 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
Andrea Corbellini
  • 17,339
  • 3
  • 53
  • 69
Philcinbox
  • 77
  • 2
  • 11
  • 4
    Just quote the var: `echo "$ifconfigvar"`. See [shell parameter expansion](https://www.gnu.org/software/bash/manual/bash.html#Shell-Parameter-Expansion). – randomir Aug 18 '17 at 22:10
  • By the way -- note that `ifconfig` is effectively abandonware, and has not been maintained on Linux for over a decade. You should really, **really** be using iproute2 (the `ip` command, as in `ip addr list` or `ip link list`) instead. Some content added in modern kernels but not supported in decade-old ones `ifconfig` doesn't know how to print at all and will just silently ignore (anonymous secondary addresses, for instance). – Charles Duffy Aug 18 '17 at 22:38

1 Answers1

3

Just do the following:

ifconfigvar=$(ifconfig eth0) && echo "$ifconfigvar"

not using quotes will make that every line break for some reason gets changed to a simple space, with quotes you avoid this.

halfer
  • 19,824
  • 17
  • 99
  • 186
poucel95
  • 46
  • 4
  • 4
    The "some reason" is parameter expansion, as pointed out by @randomir's comment. Parameters are splitted by whitespace (not just new lines) – Andrea Corbellini Aug 18 '17 at 22:18
  • Thanks for pointing that out, it's always good to learn something new – poucel95 Aug 18 '17 at 22:20
  • See https://stackoverflow.com/help/how-to-answer. From the "Answer Well-Asked Questions" section, note the bullet point that questions that "have already been asked and answered many times before" should not be answered. This, in particular, is **very** much a FAQ. – Charles Duffy Aug 18 '17 at 22:34