【问题标题】:Assign result of command in a variable在变量中分配命令的结果
【发布时间】:2016-04-15 11:03:36
【问题描述】:

我今天在变量中分配命令的结果时得到了这个奇怪的结果。

这个命令:

git branch | grep 480 

给我这样的结果:

branch_name_480

鉴于branch_name_480 是其中唯一包含 480 的分支。

但是当我尝试这样做时:

temp=`git branch | grep 480`

或者这个:

temp=$(git branch | grep 480)

然后:echo $temp 这并没有给我预期的结果——应该和以前一样。相反,这在一行中给了我类似all my directory listing and the expected result 的结果。

我知道我可以这样做以获得预期的结果:

temp=$(echo 'git branch | grep 480')

那么,我的问题是为什么会这样?为什么我之前没有得到预期的结果?

【问题讨论】:

  • 这确实很奇怪;您是否在脚本的另一个位置使用相同的变量 temp
  • 你在命令行上得到这些结果了吗?如果你只在命令行上执行这两个命令,你会得到什么:temp=$(git branch | grep 480)echo $temp
  • 这是我正在执行的唯一代码。之前或之后没有代码。只需分配和回显。

标签: git bash ubuntu grep


【解决方案1】:

使用echo "$temp"

git branch 的输出包含一个星号,shell 将其扩展为目录列表。引用将阻止它这样做。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-10
    • 2011-01-23
    • 1970-01-01
    • 2012-06-20
    • 2013-05-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多