1

When I run the command:

git lg --since="24 hours ago" | tail -1

I get the expected result:

* f71da17 - blah blah blah (12 hours ago)

However, when I store this output in a variable and echo it to the console:

last_commit=$(git lg --since="24 hours ago" | tail -1); echo $last_commit

I get the unexpected result of:

dir1/ dir2/ dir3/ file1 file2 file3 * f71da17 - blah blah blah (12 hours ago)

It prepends every file in the current directory to the output. Any insight as to what's going on would be much appreciated!

1
  • The git command is actually a set of commands surrounding git and ending with the specific command following git. What does git lg mean? Commented Aug 18, 2013 at 3:30

1 Answer 1

4

The * in the variable's value is being glob expanded because you didn't quote the expansion.

Use echo "$last_commit"

Sign up to request clarification or add additional context in comments.

7 Comments

Though, I might put the quotes around the assignment.
@Dru: that won't help; the assignment doesn't undergo word-splitting or pathname expansion anyway, so quotes are unnecessary.
from the bash man page, "If the [process] substitution appears within double quotes, word splitting and filename expansion are not performed on the results." So, if not enclosed in double quotes....
@Dru: while I think it's totally cool that you read the bash man page, you shouldn't interpolate words into it. The sentence you quote is literally correct, but it does not imply that they will be performed on unquoted command substitutions in a context in which those things would not happen on unqouted strings, and variable assignment is one of those. See step 4 in "Simple Command Expansion": "The text after the = ... undergoes tilde expansion, parameter expansion, command substitution, arithmetic expansion, and quote removal before being assigned to the variable."
@Dru: or you could just try it. 1: a=*; echo "$a" 2: a="*"; echo $a
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.