My development environment setup is such that it requires me to have certain host-name to localIP entry in /etc/host file .( Reason being multiple micro-services and storage i.e Cassandra and Redis bound to host-name ) I have created this simple bash script
newIP=`ip a | grep wlp5s0 | grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b" | head -1`
echo $newIP
oldIP=`ping -q -c 1 -t 1 dummy | grep PING | sed -e "s/).*//" | sed -e "s/.*(//"`
echo $oldIP
sed -i 's/$oldIP/$newIP/g' /etc/hosts
Althrough this is printing correct ip but the replacement is not happening , looks to me as an issue with sed but not sure