You need to exit the $ sign in the final echo command, or the $HOST_IPS variable will be replaced on the command line before the subshell is issued:
/usr/bin/bash -c "HOST_IPS=$(/usr/bin/ifconfig | /usr/bin/awk 'BEGIN {cnt=0} {if($0 ~ /inet / && cnt==1) {print $2} cnt++}'); echo \$HOST_IPS"
For more immediate visibility:
# v-- insert backslash here /usr/bin/bash -c "HOST_IPS=$(same as before); echo \$HOST_IPS"
Unlike @gniourf_gniourf's comment, there really is no need to avoid other dollar signs. However, as written, substitution substitution is not performed by a subshell (!); its result is replaced by the command line, which is passed to the subshell. Challenges
mypid() { echo $$; } bash -c "pid=$(mypid); echo \$pid; mypid"
demonstrate how it works: it once prints the PID of the parent shell and once complains that mypid not a known command because the subshell does not know this function.
Since running the ifconfig | awk ifconfig | awk in the parent shell is unlikely to be a problem; you can probably leave the replacement part of the commands unchanged. If it is important that the command is run by a subshell, you will also have to avoid all things.
source share