简体   繁体   English

无法将 git 分支命令的 output 捕获到 bash 变量中

[英]Cannot capture output of git branch command into bash variable

Very wierd.很奇怪。 If I run this command interactively, I see the expected result:如果我以交互方式运行此命令,我会看到预期的结果:

    git branch --contains c46b341b72509a02ee1c1a489a7f9b37d477c4f5
    * v2Tunnel

But if I try to capture this in a variable, what gets captured is the names of some (but not all) files in the current folder, followed by the expected branch name (v2Tunnel is not a file in my directory)但是,如果我尝试在变量中捕获它,则捕获的是当前文件夹中某些(但不是全部)文件的名称,然后是预期的分支名称(v2Tunnel 不是我目录中的文件)

    CCC=$(git branch --contains c46b341b72509a02ee1c1a489a7f9b37d477c4f5)
    # OR
    CCC=`git branch --contains c46b341b72509a02ee1c1a489a7f9b37d477c4f5`

    echo $CCC
    IWiNS README.md cicommit citest v2Tunnel

Other git commands to not have this result:其他 git 命令没有此结果:

    CCC=`git rev-parse --abbrev-ref HEAD`
    echo $CCC
    v2Tunnel

You captured * v2Tunnel in CCC so when you do echo $CCC the shell interprets that * and lists local files.您在CCC中捕获了* v2Tunnel ,因此当您执行echo $CCC时,shell 会解释该*并列出本地文件。

To prevent shell to interpret * use double quotes:为防止 shell 解释*使用双引号:

echo "$CCC"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM