Store stderr output in bash variable
Posted: Thu Mar 03, 2011 4:26 pm
[PROBLEM]
Sometimes I needed to evaluate information output to stderr in a bash script, but how to capture that output?
[SOLUTION]
Luckily, I stumbled over a comment on "stackoverflow" about "redirecting stderr to a variable in a bash script".
The solution is as follows:
Redirect stderr to stdout and redirect stdout to /dev/null.
In Bash-speak, that is:
Sometimes I needed to evaluate information output to stderr in a bash script, but how to capture that output?
[SOLUTION]
Luckily, I stumbled over a comment on "stackoverflow" about "redirecting stderr to a variable in a bash script".
The solution is as follows:
Redirect stderr to stdout and redirect stdout to /dev/null.
In Bash-speak, that is:
Code: Select all
cmd="whatever command you want to execute"
ERROR=$($cmd 2>&1 >/dev/null)
echo $ERROR