Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grep into variable and maintain stdout?

Tags:

linux

grep

bash

I've got a long running process and I want to capture a tiny bit of data from the big swath of output.

I can do this by piping it through grep, but then I can't watch it spew out all the other info.

I basically want grep to save what it finds into a variable and leave stdout alone. How can I do that?

like image 305
mpen Avatar asked Aug 15 '26 08:08

mpen


1 Answers

With tee, process substitution, and I/O redirection:

{ var=$(cmd | tee >(grep regexp) >&3); } 3>&1
like image 164
oguz ismail Avatar answered Aug 17 '26 23:08

oguz ismail



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!