In my bash script, I read in a set of lines that look like this
arg $PROG arg arg
I want to be able to run the line, capture the STDERR as a variable, and prevent either the STDOUT or STDERR from printing to the screen. This is my solution so far but the error variable is always empty.
$PROG=/c/Program1
{ error=$($(eval $line) 2>&1 1>&$out); } {out}>&1
echo $error
Please explain solutions thoroughly. I am new to bash shell scripting and trying to learn.
For a command that prints to both stdout and stderr like this one:
mycommand () { echo "stdout"; echo "stderr" >&2; }
If I issue that normally, I see all the output:
$ mycommand
stdout
stderr
I can redirect stdout or stderr to /dev/null so it doesn't get printed:
$ mycommand >/dev/null    # Equivalent to 1>/dev/null
stderr
$ mycommand 2>/dev/null
stdout
If I want to capture only stderr, I first have to redirect stderr to where stdout out is pointing to at the moment, then redirect stdout to /dev/null:
error="$(2>&1 1>/dev/null mycommand)"
Now error only contains output from stderr:
$ echo "$error"
stderr
The position on the line doesn't really matter, but the order of redirections does, so this has the same effect:
error="$(mycommand 2>&1 1>/dev/null)"
but this doesn't:
error="$(1>/dev/null 2>&1 mycommand)"
The last command would redirect both stdout and stderr to /dev/null, and error would be empty.
A great resource for redirection is on the Bash Hackers Wiki.
A few pointers for what you tried:
$ in the parameter name, so it should be PROG=/c/Program1 and not $PROG=/c/Program1.eval, see BashFAQ/048 why that is a good idea.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With