Using Bash, I want to inspect the output on the terminal but at the same time feed that output to another command for further processing. Here I use tee
#!/bin/bash
function printToStdout() {
echo "Hello World is printed to stdout"
echo "This is the second line"
}
printToStdout | tee >(grep Hello)
Output:
Hello World is printed to stdout
This is the second line
Hello World is printed to stdout
So far so good. But I need the grep
result to pass into more than 1 custom function for further processing. Hence, I try to store that to a variable by using command substitution inside tee
:
printToStdout | tee >(myVar=`grep Hello`)
echo "myVar: " $myVar
Expected Output
Hello World is printed to stdout
This is the second line
myVar: Hello World is printed to stdout
But what I got is an Unexpected Missing Output for myVar
Hello World is printed to stdout
This is the second line
myVar:
Question: how can I get the output of grep into myVar so that I can further process it?
#!/bin/bash
function printToStdout() {
echo "Hello World is printed to stdout"
echo "This is the second line"
}
{ myVar=$(printToStdout | tee >(grep Hello) >&3); } 3>&1
echo "myVar: " $myVar
Result:
Hello World is printed to stdout
This is the second line
myVar: Hello World is printed to stdout