In zsh
:
% RESPONSE=$(ping -c 1 raspberrypi4b.local)
% echo $RESPONSE | grep "% packet loss"
1 packets transmitted, 1 packets received, 0.0% packet loss
But in bash
it's different:
$ RESPONSE=$(ping -c 1 raspberrypi4b.local)
$ echo $RESPONSE | grep "% packet loss"
PING raspberrypi4b.local (192.168.1.184): 56 data bytes 64 bytes from 192.168.1.184: icmp_seq=0 ttl=64 time=1.376 ms --- raspberrypi4b.local ping statistics --- 1 packets transmitted, 1 packets received, 0.0% packet loss round-trip min/avg/max/stddev = 1.376/1.376/1.376/0.000 ms
Yet when I run ping
directly, the output is formatted identically:
In zsh
:
% ping -c 1 raspberrypi4b.local
PING raspberrypi4b.local (192.168.1.184): 56 data bytes
64 bytes from 192.168.1.184: icmp_seq=0 ttl=64 time=7.526 ms
--- raspberrypi4b.local ping statistics ---
1 packets transmitted, 1 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 7.526/7.526/7.526/0.000 ms
in bash
:
$ ping -c 1 raspberrypi4b.local
PING raspberrypi4b.local (192.168.1.184): 56 data bytes
64 bytes from 192.168.1.184: icmp_seq=0 ttl=64 time=7.613 ms
--- raspberrypi4b.local ping statistics ---
1 packets transmitted, 1 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 7.613/7.613/7.613/0.000 ms
It seems that bash
is ignoring the newlines that are in the output of ping
, but perhaps the variable assignment has caused this? In either case, can I get bash
to handle this string in the same way that zsh
does?
bash
use$ echo "$RESPONSE" | grep "% packet loss"
. Interesting logic... spend a quarter to save a dime LOL – Seamus Apr 14 '20 at 20:09