I am writing a zsh script that captures a hostname from file README
and ensures that it exists on the network by pinging it. The following is my code:
HOSTNAME=$(cat README 2>/dev/null | grep -oP "^Host(name)*:[\s]+\K(.*)")
ping -w 5 -c 1 $HOSTNAME >/dev/null 2>&1
if [ $? != 0 ]; then
# error
else
# all good
fi
I noticed that if the line that contains the hostname in README
has a trailing space, ping
doesn't work. For example, the line could look like the following where I represent white space with an _
character.
Hostname:____bobscomputer_
Does zsh not get rid of extra whitespace in its commands like bash does?