Bash how do you capture stderr to a variable?

stackoverflow picture stackoverflow · Jun 18, 2012 · Viewed 50k times · Source

Bash how do you capture stderr to a variable?

I would like to do something like this inside of my bash script

sh -c path/myExcecutable-bin 2>&1 =MYVARIABLE

How do you send stderror output to a variable ?

Answer

Tim Pote picture Tim Pote · Jun 18, 2012

To save both stdout and stderr to a variable:

MYVARIABLE="$(path/myExcecutable-bin 2>&1)"

Note that this interleaves stdout and stderr into the same variable.

To save just stderr to a variable:

MYVARIABLE="$(path/myExcecutable-bin 2>&1 > /dev/null)"