How to catch stdout and stderr in different variables?
Tested on Debian wheezy using GNU bash, Version 4.2.37 (1)-release (i486-pc-linux-gnu). This is for catching stdout and stderr in different variables. If you only want to catch stderr, leaving stdout as-is, there is a better and shorter solution.
How do I redirect output to a variable in shell?
The command outputs the modified content and would commonly be redirected into a file or piped to another command. (E.g. sed, awk, perl, etc.) Putting the read and the mystic_command into a “sub shell” via parenthesis is not necessary but makes it flow like a continuous pipe as if the 2 commands where in a separate script file.
How to convert stdout to stderr in Bash?
We have following written to stderr: where out is from $1, 2a is from stdout of dummy, err is from $2, 3b is from stderr of dummy, and the 1 is from the return code from dummy.
How to save stdout and stderr in Bash?
Ok, it got a bit ugly, but here is a solution: where (echo std; echo err >&2) needs to be replaced by the actual command. Output of stdout is saved into the array $t_std line by line omitting the newlines (the -t) and stderr into $t_err.
How to redirect errors to standard output in Bash?
You’d have to build the entire pipeline into the sub-shell, eventually sending its final standard output to a file, so that you can redirect the errors to standard output. Note that the semi-colon is needed (in classic shells – Bourne, Korn – for sure; probably in Bash too).
How to store stderr output in a variable?
EDIT: an alternative version of the capture function which stores the captured STDERR output into a user-specified variable (instead of relying on a global $captured ), taking inspiration from Léa Gris’s answer while preserving the ksh (and zsh) compatibility of the above implementation: capture choice dialog –menu “Pick one!”