Escaping dots in bash variables
bash, shell
Solution
`bash` parameter expansion supports pattern substitution, which will look (slightly) cleaner and doesn't require a call to `sed`:
echo ${ip_addr//./\\.}
Problem
I want to escape dots from an IP address in Unix shell scripts (bash or ksh) so that I can match the exact address in a grep command. ``` echo $ip_addr | sed "s/\./\\\./g" ``` works (outputs 1\.2\.3\.4), but ``` ip_addr_escaped=`echo $ip_addr | sed "s/\./\\\./g"` echo $ip_addr_escaped ``` Doesn't (outputs 1.2.3.4) How can I correctly escape the address? Edit: It looks like ``` ip_addr_escaped=`echo $ip_addr | sed "s/\./\\\\\\\./g"` ``` works, but that's clearly awful!