Bash脚本捕获输出到终端

JDS*_*JDS 10 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
Run Code Online (Sandbox Code Playgroud)

我100%肯定当我跑...

> the_command ARG1
Run Code Online (Sandbox Code Playgroud)

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

感谢您的任何帮助!

Sha*_*hin 17

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

TEST_OUT=$(the_command ARG1 2>&1)
Run Code Online (Sandbox Code Playgroud)