【问题标题】:display a variable between simple quote在简单引号之间显示一个变量
【发布时间】:2019-12-07 20:51:13
【问题描述】:

我需要在 bash 脚本中运行命令。这是我的代码和运行命令的示例:

command(){
    $(randomCommand -a '${1}')
}

command "Hello world"

其实最终的命令是这样的:

randomCommand -a '${1}'

但我想要这个:

randomCommand -a 'Hello world'

这很正常,变量不会在单引号之间进行解释,但我的程序需要这些单引号。所以...我只需要用反斜杠转义引号? 如果我像这样使用反斜杠\:

command(){
    $(randomCommand -a \'${1}\')
}

command "Hello world"

命令看起来像这样:

randomCommand -a \'Hello world\'

这根本不是我想要的……

有人知道解决方案吗?谢谢!

【问题讨论】:

  • 你好 Siggg,欢迎来到 Stackoverflow。你试过用双引号吗?喜欢这个"'${1}'"

标签: bash variables quotes backslash


【解决方案1】:
command(){
    $(randomCommand -a "'${1}'")
}

command "Hello world"

应该导致:

randomCommand -a 'Hello world'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多