JDS
JDS

Reputation: 16968

Bash script capturing output to terminal

I want to capture into my bash script (in a variable) the output of some command that prints its output to terminal. I have tried the following:

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

I am 100% sure that when I run...

> the_command ARG1

...in a terminal, it prints to the terminal exactly the information I want to capture.

Thank you for any help!

Upvotes: 11

Views: 17039

Answers (1)

Shawn Chin
Shawn Chin

Reputation: 86844

If the output is being sent to stderr, you'll need to redirect that to stdout before it can be capture in your var. Try:

TEST_OUT=$(the_command ARG1 2>&1)

Upvotes: 19

Related Questions