Leading spaces from value of a variable can be removed as follows:
$ echo $var | sed 's/^ *//g'
Trailing spaces from value of a variable can be removed as follows:
$ echo $var | sed 's/ *$//g'