8
${$(git rev-parse HEAD):0:5}
bash: ${$(git rev-parse HEAD):0:5}: bad substitution

git rev-parse HEAD returns the hash id, but how do I make a substring out of it?

if I divide it into two lines, it works.

x=$(git rev-parse HEAD)
echo ${x:0:5}

But How do I do it in one line?

2 Answers 2

21

Using --short option:

$ git rev-parse --short=5 HEAD
90752

$ x=$(git rev-parse --short=5 HEAD)
$ printf '%s\n' "$x"
90752
3

The cut utility should do what you want.

$ x=$(git rev-parse HEAD | cut -c1-5) && echo $x

Why one line?

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .