Why do you "need" to put in a variable?
I think this is questionable, but anyway ...
It seems the best way is to follow this suggestion link :
Run everything in a sub-shell and redirect the output of errors to the standard output, which is captured by the ERRORS variable. In the sub-shell, the default output is ignored (redirected to / dev / null). It's interesting.
ERRORS=$( (tar -zcf teste.tar.gz teste > /dev/null) 2>&1 )
PS 1 : just like in the English OS, I did not test the solution ... PS 2 : keys are used in the original to run the sub-shell, but I think the most common is to use parentheses same as PS 3 : in the original it has a ';' before the internal parenthesis, so ...teste ; )
, I do not know if I really need it