Why do you "need" to put in a variable ?
I find it questionable, but anyway...
It seems the best way is to follow this suggestion of https://stackoverflow.com/questions/962255/how-to-store-standard-error-in-a-variable-in-a-bash-script:
Run everything in a sub-shell and redirect the error output to the standard output, which is captured by the ERRORS variable. In the sub-shell, the default output is ignored (redirected to /dev/null). Interesting.
ERRORS=$( (tar -zcf teste.tar.gz teste > /dev/null) 2>&1 )
PS 1: as in the OS in English, I have not tested the solution...
PS 2: in the original are used {} keys to run the subshell, but I think the most common is to use same parentheses
PS 3: in the original has a ';' before the inner parenthesis, thus ...teste ; )
, I don’t know if you really need to
I ran this code that you gave me but did not print the errors in the variable, it was empty!
– Alan PS
@A.S.: Perhaps the variable was empty because the command
tar
displays a 'silent' behavior when successful.– Lacobus
If you put a -v in
tar -zcvf
it will print on STDOUT even if tar runs successfully– Vinicius Placco