darko.pp
darko.pp

Reputation: 301

Put command output into string

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.

Upvotes: 24

Views: 46476

Answers (4)

Dmitry Grinko
Dmitry Grinko

Reputation: 15214

echo "Output of my command:" $(command)

Upvotes: 0

xjcl
xjcl

Reputation: 15309

Just to add to the other answers, you can use the command output directly in another command without assigning it to an intermediate variable. Example:

wget https://feeds.npr.org/510289/podcast.xml -O podcast_`date +%Y-%m-%d`.xml 

is short for

TODAY=`date +%Y-%m-%d`
wget https://feeds.npr.org/510289/podcast.xml -O podcast_${TODAY}.xml 

and today evaluates to

wget https://feeds.npr.org/510289/podcast.xml -O podcast_2020-11-29.xml 

Upvotes: 5

Flash
Flash

Reputation: 16703

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

Upvotes: 14

phihag
phihag

Reputation: 287865

str=$(command)

Upvotes: 33

Related Questions