Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash script capturing output to terminal

Tags:

string

bash

shell

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!

like image 705
JDS Avatar asked Aug 03 '12 17:08

JDS


People also ask

How do I show output in terminal?

You can direct output from a program to your terminal by issuing the ALLOCATE command and substituting an asterisk (*) for a data set name.

How do I export output from text to terminal?

the shortcut is Ctrl + Shift + S ; it allows the output to be saved as a text file, or as HTML including colors!

How do I redirect a bash output?

The append >> operator adds the output to the existing content instead of overwriting it. This allows you to redirect the output from multiple commands to a single file. For example, I could redirect the output of date by using the > operator and then redirect hostname and uname -r to the specifications.


1 Answers

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)
like image 142
Shawn Chin Avatar answered Oct 25 '22 02:10

Shawn Chin