簡體   English   中英

重用命令在 bash 中進行管道傳輸

[英]Reuse a command to pipe in bash

我使用以下內容來縮進配置腳本的輸出:

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

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

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

我試圖將sed放在這樣的變量中:

indent=sed "s/^/    /"

然后做

./configure | indent

但這不起作用 - 我怎樣才能做到這一點?

使用 BASH 數組來保存 sed 命令:

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

然后使用:

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

或者為此 sed 命令使用一個函數:

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

然后使用:

./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