Can git-bash and cygwin shell do the same things?

12

1

On Windows 10, can git-bash and cygwin shell do the same things?

What can one do but the other can't?

For example,

  1. As shells, can they both work the same as bash?
  2. What programs and commands can run in one but not in the other?

    For example, in git-bash, I can't run some Windows command:

    $  reg add "HKCU\Software\Microsoft\VisualStudio\14.0_Config\MSBuild" /v EnableOutOfProcBuild /t REG_DWORD /d 0 /f
    ERROR: Invalid syntax.
    Type "REG ADD /?" for usage.
    

    But in Cygwin, it runs well

    $  reg add "HKCU\Software\Microsoft\VisualStudio\14.0_Config\MSBuild" /v EnableOutOfProcBuild /t REG_DWORD /d 0 /f
    The operation completed successfully.
    

    originally I thought that git-bash and cygwin can both run programs in Windows. So Why doesn't git-bash work, while cygwin can?

Thanks.

Tim

Posted 2017-03-23T23:29:40.973

Reputation: 12 647

Answers

9

The difference is that Cygwin knows how to process so-called "Win32" pathnames. Loosely speaking, it knows that "\" is a pathname separator and not a shell escape character like it is in Bash. The error you showed is Bash interpreting "\" as an escape character. (Edit: you might try replacing one \ with two, thus escaping the backslash, so that Bash passes the command through to reg.exe correctly.)

Having said that, while Cygwin groks Windows pathnames, it doesn't like to do so. The documentation warns you off from using them. Of course, a little back-sliding now and then doesn't hurt. But while you can run Windows programs in Cygwin, hygiene suggests you should run Windows programs in the CMD.EXE processor and UNIX ones in Cygwin for your long-term sanity.

AlwaysLearning

Posted 2017-03-23T23:29:40.973

Reputation: 1 169

1

You can use cygpath to convert between windows and unix path formats.

– DavidPostill – 2017-03-24T14:40:19.670

Thanks. The pathname in my commands are used as keys in the registry table, so can "" in them be replaced with "/"? – Tim – 2017-03-24T19:05:16.093

Maybe, but see the suggestion in the (newly edited) answer. – AlwaysLearning – 2017-03-25T17:03:23.130

0

I just found a difference: I can run this script in Git bash but not in Cygwin.

for file in *.xml; do
    echo "Processing file: $file"
    sed -r 's:<Autorizado>([0-9]+)</Autorizado>:<Autorizado>00013069</Autorizado>:' -i "$file"
    # ...
done

In Git bash, it process my xml files, but in Cygwin, it gives me error:

Processing file: *.xml
sed: cannot read *.xml: No such file or directory

bash --version in Git bash:

GNU bash, version 4.3.42(5)-release (x86_64-pc-msys)

in Cygwin:

GNU bash, versión 4.3.46(7)-release (x86_64-unknown-cygwin)

WesternGun

Posted 2017-03-23T23:29:40.973

Reputation: 300

This just suggests that there is no file ending in .xml in the current directory the case of the cygwin test. You should show the result of ls in the current directory with the command. – Peter - Reinstate Monica – 2019-01-25T14:35:49.700

I believe this is not the case since I am running the same script in two terminals at the same time. The file is there. – WesternGun – 2019-01-29T09:21:33.117

2There are two possibilities: (1) There is no such file, perhaps because the cygwin script runs in a different directory. In that case the shell leaves the pattern alone (instead of replacing it with the empty string). (That sounds like an almost-always mistake until one realizes that one can pass patterns to find that way unquoted.) (2) The files' names are actually *.XML (in capitals) and the shell option nocaseglob is set in the git shell making globs case insensitive (which makes a certain sense on Windows). – Peter - Reinstate Monica – 2019-01-29T15:46:25.233