Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git alias with conditional check

Tags:

git

bash

shell

sh

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.

like image 458
Eugeniu Rosca Avatar asked Apr 08 '26 20:04

Eugeniu Rosca


2 Answers

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

like image 144
CodeWizard Avatar answered Apr 10 '26 11:04

CodeWizard


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 '
like image 28
choroba Avatar answered Apr 10 '26 12:04

choroba



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!