Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash how do you capture stderr to a variable? [duplicate]

Tags:

bash

stderr

People also ask

How do I redirect stderr to a file?

Redirecting stdout and stderr to a file: The I/O streams can be redirected by putting the n> operator in use, where n is the file descriptor number. For redirecting stdout, we use “1>” and for stderr, “2>” is added as an operator.

How do I redirect stderr and stdout in Bash?

Understanding the concept of redirections and file descriptors is very important when working on the command line. To redirect stderr and stdout , use the 2>&1 or &> constructs.

What is the redirect for stderr?

The regular output is sent to Standard Out (STDOUT) and the error messages are sent to Standard Error (STDERR). When you redirect console output using the > symbol, you are only redirecting STDOUT. In order to redirect STDERR, you have to specify 2> for the redirection symbol.


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