Bash script to convert IP Addr string to hexadecimal

IP_ADDR=192.168.1.128

printf '% 02X' $ {IP_ADDR //./}; echo

Can someone explain how this simple oneliner converts IP_ADDR to hexadecimal? I hit my head trying to find documentation of this behavior.

+7
source share
2 answers

Enhancing Shell Settings

 $ IP_ADDR=192.168.1.128 $ echo ${IP_ADDR//./ } 192 168 1 128 $ printf '%02X' 192 168 1 128 ; echo C0A80180 
+8
source

You can simply use gethostip (from syslinux-utils on Debian / Ubuntu):

 $ gethostip -x 192.168.1.128 C0A80180 
0
source

All Articles