显示 Bash 命令的输出并将输出保存在变量中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2081830/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 21:33:53  来源:igfitidea点击:

Display output of a Bash command and keeping the output in a variable

bashstdoutbackticks

提问by Selmak

I'm not sure if it is possible but what I want to do is to run a bash command and storing the output in a variable AND display it as if I launched the command normally. Here is my code:

我不确定是否有可能,但我想要做的是运行 bash 命令并将输出存储在变量中并显示它,就像我正常启动命令一样。这是我的代码:

VAR=`svn checkout $URL`

So I want to store the output in VAR and still see the result (and because svn checkout takes a long time, I can't do echo $VAR just after..)

所以我想将输出存储在 VAR 中并仍然看到结果(并且因为 svn checkout 需要很长时间,我不能在之后执行 echo $VAR ..)

Thanks

谢谢

回答by Alok Singhal

If the command is run from a terminal, you can do:

如果命令是从终端运行的,您可以执行以下操作:

VAR=$(svn checkout $URL | tee /dev/tty)

回答by Paused until further notice.

You don't have to call the external tee:

您不必致电外部tee

VAR=$(svn checkout $URL) && echo $VAR

or even:

甚至:

VAR=$(svn checkout $URL); echo $VAR