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) 
2
  • 4
    Just quote the var: echo "$ifconfigvar". See shell parameter expansion. Commented Aug 18, 2017 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). Commented Aug 18, 2017 at 22:38

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

3 Comments

The "some reason" is parameter expansion, as pointed out by @randomir's comment. Parameters are splitted by whitespace (not just new lines)
Thanks for pointing that out, it's always good to learn something new
See 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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.