【发布时间】:2014-12-06 11:31:47
【问题描述】:
我想获取最新的 git commit hash 的前 8 个字符。要检索 git HEAD 哈希,我使用 git rev-parse HEAD。我发现here 可以使用${string:position:length} 获取子字符串。但我不知道如何将它们结合起来(如果可能的话)。我的尝试
${"`git rev-parse HEAD`":0:8}
错了。
【问题讨论】:
我想获取最新的 git commit hash 的前 8 个字符。要检索 git HEAD 哈希,我使用 git rev-parse HEAD。我发现here 可以使用${string:position:length} 获取子字符串。但我不知道如何将它们结合起来(如果可能的话)。我的尝试
${"`git rev-parse HEAD`":0:8}
错了。
【问题讨论】:
您不能通过在其中调用命令来组合 BASH 子字符串指令:
您可以使用:
head=$(git rev-parse HEAD | cut -c1-8)
否则老faishon 2个步骤:
head=$(git rev-parse HEAD)
head=${head:0:8}
【讨论】:
使用sed:
git rev-parse HEAD | sed 's/^\(.\{,8\}\).*$/\1/g'
【讨论】:
out=`git rev-parse HEAD`
sub=${out:0:8}
example:
a="hello"
b=${a:0:3}
bash-3.2$ echo $b
hel
这是一个两步过程,首先提取git 命令的输出,这是一个字符串。 ${string:pos:len 将返回长度为 len 的 pos 的子字符串
【讨论】:
git 命令的输出,这是一个字符串。 ${string:pos:len" 将返回来自 pos 的子字符串,长度为 len