With any awk using any value of FS and any value of OFS if your spaces are all blank chars as handled by the code in your question, here's how to do it briefly and explicitly as requested in your question:
gsub(/ +/, " ", line)
gsub(/^ | $/, "", line)
For example, lets say you have a CSV and want to print the number of fields in each line followed by the fields separated by |s. A sample input file would be:
$ cat file
stuff,nonsense
and the awk script to process that would be:
$ awk -v FS=',' -v OFS='|' '
{ print NF, $1, $2 }
' file
2|stuff|nonsense
Now let's introduce your line variable and it's associated handling (I added < and > to the output to show that the leading/trailing spaces were stripped):
$ awk -v line=' foo bar ' -v FS=',' -v OFS='|' '
{ print NF, $1, $2 }
END {
gsub(/ +/, " ", line)
gsub(/^ | $/, "", line)
print "<" line ">"
}
' file
2|stuff|nonsense
<foo bar>
and as you can see everything works exactly as intended while all of the other solutions posted so far would fail in various ways.
If the spaces in line aren't all blanks then using a POSIX awk for any type of white space characters in line (with a non-POSIX awk replace [[:space:]] with [ \t] to catch the most common chars of blank and tab, add others as you like):
gsub(/[[:space:]]+/, " ", line)
gsub(/^ | $/, "", line)
Your script:
gsub(/^ +| +$/,"", line);
gsub(/ {2,}/, " ", line);
was lengthier than it had to be because you're doing the gsub()s in the wrong order which necessitates the +s in the first one and unnecessarily checking for 2 or more blanks ({2,}) in the second one. It also wouldn't work if some of the spaces were tabs or some other white space characters.