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:
- Assignment requires to not use the
$
in the parameter name, so it should be PROG=/c/Program1
and not $PROG=/c/Program1
.
- Uppercase variable names are discouraged because they are more likely to clash with environment variables.
- In 99% of the cases, there is a solution that avoids
eval
, see BashFAQ/048 why that is a good idea.