如何将 bash 命令输出到标准输出并同时通过管道传输到另一个命令?

How to output bash command to stdout and pipe to another command at the same time?

我在服务器上工作并显示详细的 GPU 信息我使用这些命令:

nvidia-smi
ps -up `nvidia-smi |tail -n +16 | head -n -1 | sed 's/\s\s*/ /g' | cut -d' ' -f3` 

然而,如您所见,nvidia-smi 被调用了两次。如何使 nvidia-smi 的输出同时转到输出和管道到另一个命令?

使用tee:

ps -up `nvidia-smi |tee /dev/stderr |tail -n +16 | head -n -1 | sed 's/\s\s*/ /g' | cut -d' ' -f3` 

由于 stdout 是管道式的,您无法复制它,所以我选择了 stderr 来显示输出。

如果 /dev/stderr 不可用,请使用 /proc/self/fd/2