If I used the following command:

git log --pretty=format:"%ad %s%d"

the output is:

Tue Apr 26 11:29:24 2016 +0000 Updated configuration

If I do the following:

SIMPLE='--pretty=format:"%ad %s%d"'
git log $SIMPLE

then the output is as follows:

"Tue Apr 26 11:29:24 2016 +0000 Updated configuration"

I know that I can define alias gitl='git log --pretty=format:"%ad %s%d"'. But it's just bugging me to know why git is doing this.

Why does the output appear in quotes when using the variable substitution approach?

  • I'm not familiar enough with git to know; the first example has two dashes --pretty=... but the 2nd and 3rd examples have one dash. Are both acceptable? – Jeff Schaller May 10 '16 at 20:41
  • @JeffSchaller That's a typo. Corrected. – Kshitiz Sharma May 10 '16 at 20:43

The difference is because of some shell quoting specialties.

If you execute either of these (they are equivalent ways of quoting in the shell)

git log --pretty=format:'%ad %s%d'
git log '--pretty=format:%ad %s%d'
git log --pretty=format:%ad\ %s%d
git log --pretty=form'at:%ad %'s%d
git log --pretty=format:%ad" "%s%d
git log --pretty=format:"%ad %s%d"

git will get two arguments, the first being log and the second --pretty=format:%ad %s%d.

If you execute

SIMPLE='--pretty=format:"%ad %s%d"'

the variable SIMPLE will have the value --pretty=format:"%ad %s%d", including the double quotes.

Now if we are in zsh and you execute

git log $SIMPLE

or in bash

git log "$SIMPLE"

git will see the second argument as --pretty=format:"%ad %s%d". (If I execute git log $SIMPLE in bash I get an error because git gets three arguments: log, --pretty=format:"%ad and %s%d").

So inside the variable you would not need the inner quotes (except if you want to pass it to eval).

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.