First lets establish some basic behavior...
test.sh
Code:
#!/bin/bash
echo "STDOUT"
echo "STDERR" 1>&2
Code:
core$ ./test.sh
STDOUT
STDERR
core$ ./test.sh 2> STDERR.txt
STDOUT
core$ ./test.sh 1> STDOUT.txt
STDERR
core$ cat STDERR.txt
STDERR
core$ cat STDOUT.txt
STDOUT
So at this point we know and can prove our basic script outputs one line to stderr and one line to stdout. You might want to use something like this for testing because sometimes behavior does change from executing several commands to calling another program due to redirection and piping.
This--
Code:
YYY=$(XXX=`{ echo -n 111; echo 222 >&2; }` 2>&1); echo $XXX $YYY
only gives me 222, which is not being echo'd at all but is being displayed on stderr... you get same result removing the echo as having it. You can tell where your stuff is coming from by redirecting the stream to a file 1>file or 2>file.
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.