如何将命令中的可变输出存储在同一时间作为打印输出? [英] How to store the output of a command in a variable at the same time as printing the output?

查看:77
本文介绍了如何将命令中的可变输出存储在同一时间作为打印输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我想回声的东西,捕捉它的一个变量,同时我看到它在我的屏幕。

Say I want to echo something and capture it in a variable, at the same time I see it in my screen.

echo "hello" | tee tmp_file
var=$(< tmp_file)

所以,现在我可以看到你好在我的终端,以及将其保存到变量 $ VAR

So now I could see hello in my terminal as well as saving it into the variable $var.

但是,有没有办法做到这一点,而无需使用临时文件? T恤似乎并没有成为解决方案,因为它说(从 T恤的人)的从标准读取输入和写入标准输出和文件的,而这里是两倍标准输出。

However, is there any way to do this without having to use a temporary file? tee doesn't seem to be the solution, since it says (from man tee) read from standard input and write to standard output and files, whereas here it is two times standard output.

我在Bash的4.3,如果这很重要。

I am in Bash 4.3, if this matters.

推荐答案

使用发球直接就直奔屏幕而不是标准输出

Use tee to direct it straight to screen instead of stdout

$ var=$(echo hi | tee /dev/tty)
hi
$ echo $var
hi

这篇关于如何将命令中的可变输出存储在同一时间作为打印输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆