24

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.

0

4 Answers 4

33
str=$(command)

Sign up to request clarification or add additional context in comments.

Comments

14

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

Comments

5

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 

Comments

0

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

1 Comment

Your answer could be improved by adding more information on how the proposed solution solves the problem and how it helps the OP.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.