Code:
set -- `users`
var2=" "
for f; do
case $var2 in *" "$f" "*) ;; *) var2=" $f$var2";; esac
done
This should work as long as the output you want to process is whitespace-separated. You can play around with IFS= to use a different separator, but in the general case, it gets rather ugly and cumbersome.
For convenience, I made it so that
$var2 will have one leading and one trailing space. Removing or ignoring those is left as an exercise.
The shell is not very good at these things; that's why
awk and
sed are more or less essential for nearly any moderately complex shell script. (Or use
Perl if that helps at all.)
Modern shells have additional facilities, including an array variable type, but this approach works even with Bourne Classic. We are stuck with it, and in fact, we kind of like it, in spite of its oddities and quirks.