开发者

Put command output into string

开发者 https://www.devze.com 2023-03-12 19:20 出处:网络
If I execute some command in a Linux shell, how can I store the output into a 开发者_如何学Pythonstring (variable) so I can use it later? I need this for a Bash script, please help.str=$(command)

If I execute some command in a Linux shell, how can I store the output into a 开发者_如何学Pythonstring (variable) so I can use it later? I need this for a Bash script, please help.


str=$(command)


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


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 


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

0

精彩评论

暂无评论...
验证码 换一张
取 消