在bash中重用命令管道

时间:2014-11-10 19:22:42

标签: linux bash shell sed

我使用以下内容来缩进配置脚本的输出:

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

现在我想重用管道后面的部分,所以我不必写

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

我试图将sed放在这样的变量中:

indent=sed "s/^/    /"

然后再做

./configure | indent

但这不起作用 - 我怎样才能做到这一点?

3 个答案:

答案 0 :(得分:7)

使用BASH数组来保存sed命令:

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

然后使用:

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

或者使用此sed命令的函数:

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

然后使用:

./configure | indent
make | indent
make install | indent

答案 1 :(得分:3)

试试这个:

(./configure; make; make install) | sed "s/^/    /"

答案 2 :(得分:2)

为什么不把它作为别名?

alias indent="sed 's/^/    /'"
相关问题