Search code examples
bashstderr

Bash how do you capture stderr to a variable?


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 ?


Solution

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