1

How to run bash script in the current directory from cmd-file by using Cygwin?

It doesn't work - my file script.cmd contains: https://stackoverflow.com/a/17204645/1558037

c:\cygwin64\bin\bash -l -c '%CD%/my_script.sh'

Output

E:\mydir>c:\cygwin64\bin\bash -l -c 'E:\mydir/my_script.sh'
/usr/bin/bash: E:mydir/my_script.sh: No such file or directory

Answer:

I can successfully use such commands:

  • c:\cygwin64\bin\bash -l -c "cd %CD:\=/%/; %CD:\=/%/my_script.sh"
  • c:\cygwin64\bin\bash -l -c "cd %CD:\=/%/; echo $PWD"
Alex
  • 12,578
  • 15
  • 99
  • 195
  • Please do not add answers to your question; if you want to share your solution just post it as an answer; thank you! – aschipfl Feb 08 '19 at 11:18

2 Answers2

2

solution in two steps, first convert the %CD% with cygpath then call bash with the converted path in POSIX format

FOR /F %%I IN ('c:\cygwin64\bin\cygpath -c -u %CD%') DO SET CDU=%%I
c:\cygwin64\bin\bash -l -c %CDU%/my_script.sh
matzeri
  • 8,062
  • 2
  • 15
  • 16
1

In the returned error message you showed the backslash between E: and mydir disappeared, which lets me assume bash uses such as escape characters.

Windows Command Prompt (cmd) however uses backslashes as path separators, hence %CD% contains such. However, bash expects forward-slashes as path separators.

Therefore, to replace all backslashes by forward-slashes, use sub-string substitution, like this:

c:\cygwin64\bin\bash -l -c '%CD:\=/%/my_script.sh'

In case the single-quotes cause troubles as well, use double-quotes:

c:\cygwin64\bin\bash -l -c "%CD:\=/%/my_script.sh"
aschipfl
  • 33,626
  • 12
  • 54
  • 99
  • 1
    I don't know if the single-quotes could also be a problem, because `cmd` uses only double-quotes, so maybe they might need to be changed too, but I can't test, sorry... – aschipfl Feb 05 '19 at 19:12