1

I am learning git for some time, recently I have been using aliases. Everything was working, until last time. My example alias stopped working ( git simple-commit works fine )

simple-loop = "!simpleLoop() { NAME=$1; i="1"; while [ $i -le $2 ]; do git simple-commit $NAME$i; i=$[$i+1]; done; }; simpleLoop"

I am getting fatal in terminal

simpleLoop() { NAME=$1; i=1; while [ $i -le $2 ]; do git simple-commit $NAME$i; i=$[$i+1]; done; }; 
simpleLoop: 1: [: Illegal number: $[1+1]

It looks like git not using bash shell. Any idea what is going on ?

  • 1
    `$[$i+1]` is valid *bash* syntax, but not valid *POSIX shell* syntax. Git uses `/bin/sh` on Unix-like systems (including Linux) and that may be a POSIX shell variant rather than a bash variant. Use the double parentheses syntax as it's much more widely implemented. – torek Nov 30 '19 at 20:26
  • `$[...]` is (and has been for a very long time) obsolete in `bash`; use `$((...))` instead. – chepner Nov 30 '19 at 20:40

1 Answers1

1

I just tested a simplified version of that alias:

aa = "!simpleLoop() { i="1"; while [ $i -le "4" ]; do echo $i; i=$[$i+1]; done; }; simpleLoop"

And git aa does give the expected result:

D:\git\>git aa
1
2
3
4

To be sure, assuming Git for Windows, test your alias in a CMD session with a simplified PATH:

set PATH=C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\
set GH=C:\path\to\git
set PATH=%GH%\bin;%GH%\usr\bin;%GH%\cmd;%GH%\mingw64\bin;%PATH%

You can also use a number for i (i=1 instead of "1") and use other syntax to increment that variable (like i=$((i+1)))

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250