【问题标题】:Put command output into string将命令输出放入字符串
【发布时间】:2011-09-13 02:16:40
【问题描述】:

如果我在 Linux shell 中执行某些命令,如何将输出存储到字符串(变量)中以便以后使用?我需要这个作为 Bash 脚本,请帮忙。

【问题讨论】:

    标签: linux string bash


    【解决方案1】:
    str=$(command)
    

    【讨论】:

      【解决方案2】:

      result=`command`result=$(command) 都将command 的输出分配给result 变量。

      【讨论】:

        【解决方案3】:

        只是为了添加其他答案,您可以直接在另一个命令中使用命令输出,而无需将其分配给中间变量。示例:

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

        简称

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

        今天评估为

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

        【讨论】:

          猜你喜欢
          • 2018-11-25
          • 1970-01-01
          • 2021-06-28
          • 1970-01-01
          • 2015-11-30
          • 1970-01-01
          • 1970-01-01
          • 2018-09-28
          • 1970-01-01
          相关资源
          最近更新 更多