Bash how do you capture stderr to a variable?

BashStderr

Bash Problem Overview


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 ?

Bash Solutions


Solution 1 - Bash

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)"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionstackoverflowView Question on Stackoverflow
Solution 1 - BashTim PoteView Answer on Stackoverflow