For the Bash shell, the ancient syntax for this is the backticks (the left quote usually above the tab key).
But the more modern syntax is the $( ... ) construct:
Code:
#old and deprecated
variable=`ifconfig eth0`
#new-er and shiny
variable=$( ifconfig eth0 )
As mentioned above, it can be difficult to parse after the fact. Also, if you try to parse it afterwards, you'll quickly pick up on some of the wonders of quoting variable references...
Code:
$ echo $variable
eth0 Link encap:Ethernet HWaddr 00:90:f5:aa:af:e5 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)
Interrupt:35 Base address:0xe000
$ echo "$variable"
eth0 Link encap:Ethernet HWaddr 00:90:f5:aa:af:e5
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)
Interrupt:35 Base address:0xe000
The first command, missing the quotes, creates the illusion that the $( ... ) command failed to capture properly, but in reality, the shell parser itself will eat whitespace on a variable when it is referenced; if the quotes are omitted. Long story short (too late
), it's almost always a good idea to quote all variable references.
Bookmarks