func() {
echo 'hello'
echo 'This is an error' >&2
}
a=$(func)
b=???
I'd like to redirect the stderr to b
variable without creating a temporary file.
echo $b
# output should be: "This is an error"
The solution that works but with a temporary file:
touch temp.txt
exec 3< temp.txt
a=$(func 2> temp.txt);
cat <&3
rm temp.txt
So the question is, how do I redirect the stderr
of the bash function func
to the variable b
without the need of a temporary file?
Best Answer
On Linux and with shells that implement here-documents with writable temporary files (like
zsh
orbash
versions prior to 5.1 do), you can do:(where
ls /dev/null /x
is an example command that outputs something on both stdout and stderr).With
zsh
, you can also do:(where
=(cmd)
is a form of process substitution that uses temporary files, and(){ code; } args
anonymous functions).In any case, you'd want to use temporary files. Any solution that would use pipes would be prone to deadlocks in case of large outputs. You could read stdout and stderr through two separate pipes and use
select()
/poll()
and some reads in a loop to read data as it comes from the two pipes without causing lock-ups, but that would be quite involved and AFAIK, onlyzsh
hasselect()
support built-in and onlyyash
a raw interface topipe()
(more on that at Read / write to the same file descriptor with shell redirection).Another approach could be to store one of the streams in temporary memory instead of a temporary file. Like (
zsh
orbash
syntax):(assuming the command doesn't output any NUL)
Note that
$err
will include the trailing newline character.Other approaches could be to decorate the stdout and stderr differently and remove the decoration upon reading:
That assumes GNU
grep
and that the lines are short enough. With lines bigger than PIPEBUF (4K on Linux), lines of the output of the twogrep
s could end up being mangled together in chunks.