Alex
Alex

Reputation: 13116

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

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:

Upvotes: 1

Views: 1432

Answers (2)

aschipfl
aschipfl

Reputation: 34899

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"

Upvotes: 1

matzeri
matzeri

Reputation: 8466

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

Upvotes: 2

Related Questions