简体   繁体   English

重用命令在 bash 中进行管道传输

[英]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:我试图将sed放在这样的变量中:

indent=sed "s/^/    /"

and then do然后做

./configure | indent

but that did not work - how can I achieve this?但这不起作用 - 我怎样才能做到这一点?

Use a BASH array to hold the sed command:使用 BASH 数组来保存 sed 命令:

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

Then use:然后使用:

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

OR else use a function for this sed command:或者为此 sed 命令使用一个函数:

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

Then use:然后使用:

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

为什么不让它成为别名?

alias indent="sed 's/^/    /'"

尝试这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM