Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Put command output into string

Tags:

string

linux

bash

If I execute some command in a Linux shell, how can I store the output into a string (variable) so I can use it later? I need this for a Bash script, please help.

like image 291
darko.pp Avatar asked Jun 11 '11 15:06

darko.pp


People also ask

How do you store output in a string?

To save the str output as a string in R, we can use capture. output function along with str function. It would be better if we save it in an object as str_df<- capture. output(str(df)) so that it can be recalled in the future in current session.

How do I copy a terminal output to a text file?

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

How do you write the output of a command 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 ...]


2 Answers

str=$(command)

like image 77
phihag Avatar answered Sep 22 '22 13:09

phihag


result=`command` or result=$(command) both assign the output of command to the result variable.

like image 22
Flash Avatar answered Sep 22 '22 13:09

Flash