How to get stderr and stdout of a program into separate env variables?
I'm looking for a more efficient way of doing something like this:
Code:
MY_STDOUT=`my-command` I've tried this: Code:
YYY=$(XXX=`{ echo -n 111; echo 222 >&2; }` 2>&1); echo $XXX $YYY Code:
222 What's wrong in my script? Maybe there exists a nicer way of doing this? |
First lets establish some basic behavior...
test.sh Code:
#!/bin/bash Code:
core$ ./test.sh This-- Code:
YYY=$(XXX=`{ echo -n 111; echo 222 >&2; }` 2>&1); echo $XXX $YYY The real problem you're having here is scoping. the XXX is being lost when it returns from calling out no matter what else you do. |
Backing up what rweaver wrote, as it says in the Bash FAQ: "What you cannot do is capture stdout in one variable, and stderr in another, using only FD redirections. You must use a temporary file to achieve that one".
|
All times are GMT -5. The time now is 01:46 PM. |