With sed , counting directly from a variable:
$ sed 's/ /_/g' <<< "$a" hello_world
And to save the result, you should use the syntax var=$(command) :
a=$(sed 's/ /_/g' <<< "$a")
For completeness, with awk this can be done as follows:
$ a="hello my name is" $ awk 'BEGIN{OFS="_"} {for (i=1; i<NF; i++) printf "%s%s",$i,OFS; printf "%s\n", $NF}' <<< "$a" hello_my_name_is
source share