I am trying to get the output of a pipe into a variable. I tried the following things:
echo foo | myvar=$(</dev/stdin)
echo foo | myvar=$(cat)
echo foo | myvar=$(tee)
But $myvar
is empty.
I don’t want to do:
myvar=$(echo foo)
Because I don’t want to spawn a subshell.
Any ideas?
Edit: I don’t want to spawn a subshell because the command before the pipe needs to edit global variables, which it can’t do in a subshell. Can it? The echo
thing is just for simplification. It’s more like:
complex_function | myvar=$(</dev/stdin)
And I don’t get, why that doesn’t work. This works for example:
complex_function | echo $(</dev/stdin)
myvar
supposed to contain? Could you give an example with a real command and explain what output you want to save? And what do you have against subshells anyway? – terdon♦ Jan 17 '17 at 10:22$myvar
does not containfoo
in my examples. After all,foo
should be in stdin. I simplified the example on purpose. Theecho foo
thing is actually a more complicated command changing global variables, which won’t work if it’s in a subshell. – Parckwart Jan 17 '17 at 10:26man bash
. Just give us a complete example and we can help you out. – terdon♦ Jan 17 '17 at 10:57