Home > OS >  How to sed output of a command and not supress output
How to sed output of a command and not supress output

Time:10-15

I have the example text stored in test.sh:

echo 'Hello world 123'
echo 'some other text' 

With the following command in a bash script:

word123=$(./test.sh |sed -nr 's/Hello world (.*)/\1/p' )

This works correctly and outputs:

123

However, this does output

Hello world 123
some other text

Is there a way to capture the text in 123 and also output everything else in the file?

CodePudding user response:

With Linux, bash and tee:

word123=$( ./test.sh | tee >&255 >(sed -nr 's/Hello world (.*)/\1/p') )

File descriptor 255 is a non-redirected copy of stdout.

See: What is the use of file descriptor 255 in bash process

  • Related