Search code examples
windowsbatch-filecommand-linecygwinworking-directory

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:

  • c:\cygwin64\bin\bash -l -c "cd %CD:\=/%/; %CD:\=/%/my_script.sh"
  • c:\cygwin64\bin\bash -l -c "cd %CD:\=/%/; echo $PWD"

Solution

  • 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"