我有一些shell命令。我想将输出写入标准输出,并将其保存到变量中。我想用一个命令来解决这个问题。我试过这些东西。
ls > $VAR # redirects the output to file which name is stored in $VAR
ls | tee -a $VAR # writes to standard output as well as in file which name is stored in $VAR
VAR=`ls` # output into $VAR, but it is not sent to standard output
VAR=`ls`;echo $VAR # ok, it works but these are two commands
有什么想法吗?
发布于 2011-10-26 19:50:58
这样如何:
VAR=$(ls | tee /dev/tty)
发布于 2013-07-30 16:18:22
@Gary_Barker给出了很好的答案,但这并不是在所有系统上都可能实现的。
在我们的团队城市中没有char-console。还有一个小问题。
如果我使用
VAR=$(ls -1); echo $VAR
它不同于ls -1
我的解决方案是有效的,如果输出来自错误管道并不重要的话。
VAR=$(ls -1 | tee >&2)
https://stackoverflow.com/questions/7901568
复制相似问题