In Bash, how to set stdout to a variable inside tee

Cupid Chan

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?

0stone0
#!/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

Try it online!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how to set a FILE** variable to stdout?

From Dev

Loading a stdout into a variable in a line using tee

From Dev

How to suppress output from tee based on variable in Bash

From Dev

Bash : How to store STDOUT (&1) pointer in a variable

From Dev

set global variable AND use stdout from bash function

From Dev

How to append tee to a file in Bash?

From Dev

How to append tee to a file in Bash?

From Dev

How to evaluate a variable inside a variable bash scripting?

From Java

How to check if a variable is set in Bash?

From Dev

How to get the environment variable set by a bash script from inside a python script?

From Dev

How to compare variable dynamically inside awk in bash?

From Dev

How redirect stderr to variable inside if condition? Bash

From Dev

How to declare a variable inside bash -c

From Dev

bash function - Get function stdout value inside a variable and modify variables outside the function

From Dev

How to share a variable that is set inside a block

From Dev

How to set the value of a liquid variable inside of Javascript

From Dev

tee stdout to stderr?

From Dev

bash, how to stdout to file

From Dev

In a bash script, how to use a variable inside a command and assign to a new variable?

From Dev

In a function Bash: how to check if an argument is a set variable?

From Dev

How to set `screen` environment variable from bash?

From Dev

How to set value of grep result to variable in bash?

From Dev

How to set variable in the curl command in bash?

From Dev

How to set a variable to a random value with bash

From Dev

How to unset a variable set with declare in bash

From Dev

How to set a bash variable in a compound xargs statement

From Dev

Variable comparison inside if in Bash

From Dev

Bash variable inside awk

From Dev

BASH variable and awk inside variable

Related Related

  1. 1

    how to set a FILE** variable to stdout?

  2. 2

    Loading a stdout into a variable in a line using tee

  3. 3

    How to suppress output from tee based on variable in Bash

  4. 4

    Bash : How to store STDOUT (&1) pointer in a variable

  5. 5

    set global variable AND use stdout from bash function

  6. 6

    How to append tee to a file in Bash?

  7. 7

    How to append tee to a file in Bash?

  8. 8

    How to evaluate a variable inside a variable bash scripting?

  9. 9

    How to check if a variable is set in Bash?

  10. 10

    How to get the environment variable set by a bash script from inside a python script?

  11. 11

    How to compare variable dynamically inside awk in bash?

  12. 12

    How redirect stderr to variable inside if condition? Bash

  13. 13

    How to declare a variable inside bash -c

  14. 14

    bash function - Get function stdout value inside a variable and modify variables outside the function

  15. 15

    How to share a variable that is set inside a block

  16. 16

    How to set the value of a liquid variable inside of Javascript

  17. 17

    tee stdout to stderr?

  18. 18

    bash, how to stdout to file

  19. 19

    In a bash script, how to use a variable inside a command and assign to a new variable?

  20. 20

    In a function Bash: how to check if an argument is a set variable?

  21. 21

    How to set `screen` environment variable from bash?

  22. 22

    How to set value of grep result to variable in bash?

  23. 23

    How to set variable in the curl command in bash?

  24. 24

    How to set a variable to a random value with bash

  25. 25

    How to unset a variable set with declare in bash

  26. 26

    How to set a bash variable in a compound xargs statement

  27. 27

    Variable comparison inside if in Bash

  28. 28

    Bash variable inside awk

  29. 29

    BASH variable and awk inside variable

HotTag

Archive