1. 程式人生 > >shell 之 tee 命令,實現重定向到檔案的同時仍能 通過管道 (|)傳給接下來的命令

shell 之 tee 命令,實現重定向到檔案的同時仍能 通過管道 (|)傳給接下來的命令

在下面的命令中 tee 收到來自 stdin 的資料 ,它將 收到的資料 一份副本寫入檔案 out.txt ,同時將另一份副本作為後續命令的 stdin ,cat -n 將從stdin 中接收到的每一行資料加上行號並 寫入 stdout .

$cat a* | tee out.txt | cat -n

tee 命令預設會覆蓋檔案,但它提供了一   -a 選項,用於追繳內容。

$cat a* | tee -a out.txt | cat -n

我們可以將 stdin 作為命令列引數 ,只需要將 - 作為命令的檔名引數即可

例如

$echo who is this | tee -

輸出結果為

who is this

who is this