2

I have a Git commit, which has a summary, and then some description. So when I see the commit message via git log --format=%B -n 1 <commit>, it looks like this:

Commit Summary Line * Commit Description Line 1 * Commit Description Line 2 * Commit Description Line 3

When I try to store this in a Bash variable, however, thus:

message=$(git log --format=%B -n 1 <commit>)

and then I try to echo $message, I get the folder names from my current directory mixed with each of the lines from the commit message. What's more, I am not even seeing all lines from the commit message, just some of them. So, $message looks something like this:

Commit Summary Line folder1 folder2 folder3 Commit Description Line 1 folder1 folder2 folder3 Commit Description Line 3

Is there any explanation for this behavior? I just want $message to have all lines from the full commit message. I don't even care if they are in new lines or all in one line, I just want all lines stored in a string variable. How do I achieve this?

1
  • 3
    As I always say, when in doubt ... quote it! Commented Dec 13, 2017 at 2:16

1 Answer 1

2

Seems to be a bit of rogue pathname expansion at play here caused by the *. Try adding a pair of quotes around your message variable and you should be good!

echo "$message" 
Sign up to request clarification or add additional context in comments.

2 Comments

In shell scripts, using variables without protecting them withdouble-quotes is almost always a mistake -- there are many ways for things to go wrong!
@GordonDavisson – wise words my friend! I cannot count all the times missing quotes have ruined my day.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.