Search code examples
linuxbashshellstdoutstderr

Redirect STDERR to a variable


I have this code that produces an error, and it works to supress the STDOUT but it doesnt store the STDERR in the variable ERROR.

ERROR = $(memtester 900 1 > /dev/null)

Solution

  • You can capture it like this:

    error=$(memtester 900 1 2>&1 >/dev/null)
    

    order of redirection operators is important here.

    • 2>&1 - redirects stderr to stdout
    • >/dev/null - redirects stdout to /dev/null