Dynamic add /etc/hosts problem

  html5, question

Correct version:

HOSTNAME=`hostname`
 ip_addr=`/sbin/ifconfig bond0 |grep -a "inet addr:" |awk -F":" '{print $2}' |egrep -o '([0-9]{1,3}\.?  ){4}'`
 echo ${ip_addr} > temp.txt
 sudo sh -c 'echo "`cat temp.txt` ${HOSTNAME}" >> /etc/hosts'

Problem version:

#!  /bin/bash
 HOSTNAME=`hostname`
 ip_addr=`/sbin/ifconfig bond0 |grep -a "inet addr:" |awk -F":" '{print $2}' |egrep -o '([0-9]{1,3}\.?  ){4}'`
 sudo sh -c 'echo "${ip_addr} ${HOSTNAME}" >> /etc/hosts'

Why does the problematic version add /etc/hosts? Finally, there is only the host name in the /etc/hosts file, ip_addr is replaced by a space, and the first one is ok?

The problematic output is:

test.test.com

The normal output is:

1.1.1.1  test.test.com

The reason for this is the following:
1. Variables in single quotation marks will not expand
2. If you want subroutines to inherit environment variables, you need to use export to define variables.
3. sudo will selectively transmit environment variables

Explanation:
1. Tryecho $USER '$USER'. This causes $ip_addr to be parsed by the sh command, triggering the second one.
2. Variables not defined by export can only work in the current script and not in subprograms.
3. HOSTNAME is already in the system environment variable. Even if you do not set it, you can directly get its value, so sh command after sudo can get the value of HOSTNAME; However, the ip_addr variable is not transmitted by sudo, so sh command after sudo cannot get the value of ip_addr variable. Sudo -E parameter allows sudo to transmit all environment variables.