Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reuse a command to pipe in bash

I use the following to indent the output of a configure script:

./configure | sed "s/^/    /"

Now I want to reuse the part behind the pipe, so I don't have to write

./configure | sed "s/^/    /"
make | sed "s/^/    /"
make install | sed "s/^/    /"

I've tried to put the sed in a variable like this:

indent=sed "s/^/    /"

and then do

./configure | indent

but that did not work - how can I achieve this?

like image 864
maxdev Avatar asked Dec 09 '22 05:12

maxdev


1 Answers

Use a BASH array to hold the sed command:

indent=(sed "s/^/    /")

Then use:

./configure | "${indent[@]}"
make | "${indent[@]}"
make install | "${indent[@]}"

OR else use a function for this sed command:

indent() { sed "s/^/    /"; }

Then use:

./configure | indent
make | indent
make install | indent
like image 128
anubhava Avatar answered Dec 11 '22 11:12

anubhava