Eugeniu Rosca Eugeniu Rosca - 7 months ago 13
Bash Question

git alias with conditional check

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.

Answer

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

Comments