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?

lebowski
  • 1,031
  • 2
  • 20
  • 37

1 Answers1

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" 
Travis Clarke
  • 5,951
  • 6
  • 29
  • 36
  • 1
    In shell scripts, using variables without protecting them withdouble-quotes is almost always a mistake -- there are many ways for things to go wrong! – Gordon Davisson Dec 13 '17 at 02:19
  • @GordonDavisson – wise words my friend! I cannot count all the times missing quotes have ruined my day. – Travis Clarke Dec 13 '17 at 02:23