Code to get the IP address and format it to run in nmap For scanning my subnet using my IP address, I need to run the following command

nmap -sn 192.168.100.*


I want to extend this functionality such that I don't have to manually specify the first three parts of the IP range myself. I came up with this rough solution.

ip=$( hostname -I | awk '{print $1}' | awk -F. '{print $1; print $2; print $3}' ORS="."; echo -n "*"); 
nmap -sn $ip


Is there a more cleaner way of achieving the same?
You can do it purely in Bash, using ${parameter%word} to strip off the last octet from the IP address.

hostname -I | while read ip _ ; do
    nmap -sn  ${ip%.*}.\*
done


Note that hostname -I is not portable: the -I option appears to be a GNU extension.