why don't you use nameserver=8.8.8.8, so changeable part is variable
no reason to keep constant value within variable
so the executing line will be: send "echo \"nameserver $nameserver\" > /etc/dnsmasq.upstream\r"