in reply to redirecting output and warning messages

With $foo=`ls` you get the output of ls (STDOUT) into the variable $foo directly, from there you can write it to whereever you want.