3

Translating this shell command:

$> sh -c 'if [ 1 -eq 1 ]; then echo TRUE; else echo FALSE; fi'
TRUE

To this git alias in ~/.gitconfig:

[alias]
    test = !sh -c 'if [ 1 -eq 1 ]; then echo TRUE; else echo FALSE; fi'

Results in:

$> git test
<output-omitted> Syntax error: Unterminated quoted string

PS: Don't want to map shell scripts to git aliases.

PS2: As @Cyrus and @CodeWizard have mentioned, one additional pair of quotes is needed to protect the alias definition against multiple levels of expansion. Nice trick to avoid semicolons provided by @choroba. Thanks everyone.

2
  • ; is a comment in .gitconfig how you escape a comment IDK. Commented Apr 23, 2016 at 19:49
  • Thanks @andlrc. I've just got the answer from the guys below. Commented Apr 23, 2016 at 20:06

3 Answers 3

8

Another solution is to put it in a string:

[alias]
    test = "!sh -c 'if [ 1 -eq 1 ]; then echo TRUE; else echo FALSE; fi'"

This will also work.

enter image description here enter image description here

1
  • 1
    This is the most dynamic answer. Commented Apr 23, 2016 at 20:59
3

It seems semicolons have special meaning in .gitconfig. Fortunately, you can rewrite the command without semicolons:

[alias]
        test = !sh -c '[ 1 -eq 2 ] && echo TRUE || echo FALSE '
1
3

Try to quote complete command:

test = "!sh -c 'if [ 1 -eq 1 ]; then echo TRUE; else echo FALSE; fi'"

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