Bash脚本捕获输出到终端

时间:2012-08-03 17:37:38

标签: string bash shell

我想将我的bash脚本(在变量中)捕获一些命令的输出,该命令将其输出打印到终端。我尝试过以下方法:

TEST_OUT=`the_command ARG1`   #Nope

#Putting the line "the_command ARG1" into a separate script, testing2.sh,

TEST_OUT=$(./testing2.sh)   #Nope

testing2.sh
TEST_OUT=$?  #Nope

我100%肯定当我跑...

> the_command ARG1

...在终端中,它会向终端打印我想要捕获的信息。

感谢您的帮助!

1 个答案:

答案 0 :(得分:17)

如果输出发送到stderr,则需要将其重定向到stdout才能在var中捕获。试试:

TEST_OUT=$(the_command ARG1 2>&1)
相关问题