【问题标题】:Command substitution in Linux - set variables (username and password) in bashrcLinux 中的命令替换 - 在 bashrc 中设置变量(用户名和密码)
【发布时间】:2014-07-29 02:28:05
【问题描述】:

我对 linux 很陌生。我想做命令替换。我想在 bashrc 中定义一个变量并将其设置为等于 linux 命令的返回值。它的语法是什么?

这正是我想要做的。我在 heroku 上部署了一个 Rails 4 应用程序,我在 Heroku 上使用 Sendgrid 发送电子邮件。现在,当我在控制台(本地文件夹 my_app_folder 中)中键入 heroku config:get SENDGRID_USERNAME(或 heroku config:get SENDGRID_PASSWORD)时,它会返回我的 Sendgrid 用户名(或密码)。我想在 baschrc 中定义一个变量 SENDGRID_USERNAME 并将其设置为等于 heroku config:get SENDGRID_USERNAME 的返回值(而不是复制粘贴实际值)。我该怎么做?类似于以下内容:

export SENDGRID_USERNAME=my_app_folder/heroku config:get SENDGRID_USERNAME

非常感谢。

【问题讨论】:

    标签: linux bash heroku sendgrid


    【解决方案1】:

    这就是你要找的吗?

    SENDGRID_USERNAME=$( path/to/heroku config:get SENDGRID_USERNAME )
    #                 ^^                                             ^
    

    在执行时,$( ... ) 被括号中的命令的输出 替换。引用man bash:

    Command Substitution
    
       Command substitution allows the output of a command to replace the com‐
       mand name.  There are two forms:
    
              $(command)
       or
              `command`
    
       Bash performs the expansion by executing command and replacing the com‐
       mand  substitution  with  the  standard output of the command, with any
       trailing newlines deleted.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-16
      • 2015-06-28
      • 2012-07-11
      • 1970-01-01
      • 2012-09-01
      • 1970-01-01
      • 2016-12-12
      • 2023-01-02
      相关资源
      最近更新 更多