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.
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 ...]
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.
Python subprocess.Using subprocess. check_output() function we can store the output in a variable.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With