Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return the output of program in a variable?

Tags:

bash

Can any one tell me how to return the output of a program in a variable from command line?

var = ./a.out params

I am trying to get the output of program into a variable while running this from command line.

like image 623
pkm Avatar asked Jul 02 '13 06:07

pkm


People also ask

How do you assign a output to a variable?

To store the output of a command in a variable, you can use the shell command substitution feature in the forms below: variable_name=$(command) variable_name=$(command [option ...] arg1 arg2 ...) OR variable_name='command' variable_name='command [option ...]

How do you return a value from a command?

The return value of a command is stored in the $? variable. The return value is called exit status. This value can be used to determine whether a command completed successfully or unsuccessfully.

How do you store output of a command in a variable in Python?

Python subprocess.Using subprocess. check_output() function we can store the output in a variable.


1 Answers

To save the program output to stdout in variable in Unix shell, regardless of language program wrote in, you can use something like this

var=`./a.out params`

or this

var=$(./a.out params)

Remember not to put spaces before or after the = operator.

like image 130
Alexey Shmalko Avatar answered Sep 27 '22 22:09

Alexey Shmalko