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.