Cygwin: Run a script in mintty/bash

6,514

Thanks to @vaz_az for keeping me motivated.

The problem with cygwin is, that it requires POSIX style paths.

This means that you have to translate the file parameter %1 that is supplied by windows. This can be done using the cygpath tool. The following code shows a 1-liner that can be used as command in regedit:

C:\cygwin\bin\mintty.exe -e /bin/bash -l -c '$(/bin/cygpath "%1")'

In the following line, there are some examples, what can be done with a 1-liner:

#Simple
C:\cygwin\bin\mintty.exe -e bash -l -c '$(cygpath "%1")'

#Fire and Forget (With 1 second delay at the end to read any messages)
C:\cygwin\bin\mintty.exe -e bash -l -c '$(cygpath "%1"); echo DONE; sleep 1'

#With logging to static file
C:\cygwin\bin\mintty.exe -l C:\cygwin\home\Nippey\cygwin.log -e /bin/bash -l -c '$(cygpath "%1")'

#With interactive shell after execution (Unfortunately the -i parameter of bash does not work together with -c, so you have to start a sub-shell)
C:\cygwin\bin\mintty.exe -e /bin/bash -l -c '$(cygpath "%1"); bash'
Share:
6,514

Related videos on Youtube

Nippey
Author by

Nippey

Updated on September 18, 2022

Comments

  • Nippey
    Nippey almost 2 years

    Initially asked on SO.com, but I closed it. I think it better fits SU.com. Please tell me if it is also not the right place.

    I want to add a context menu to a .sh file to run it in cygwin.

    I tried to manipulate the default "Open Cygwin here" command:

    C:\cygwin\bin\mintty.exe -e /bin/xhere /bin/bash.exe "%L"
    

    Unfortunately, all I get is a window that closes again immediately.
    Also, I am not 100% sure, what is the purpose and the meaning of the arguments of xhere.

    This is working:

    C:\cygwin\bin\bash.exe %1
    

    But I'd like to have mintty as terminal window.

    Final Question:
    Is there a way to add a command string to be executed to the "Open Cygwin here" string?

  • Nippey
    Nippey about 10 years
    Thanks for your feedback. It actually didn't solve my problem, as Cygwin expects Linux "/" paths and not M$ "\" paths. But you pushed my motivation to try harder! I will add a separate answer.
  • Nippey
    Nippey about 10 years
    To be more exact, using your answer leads to /bin/bash: C:cygwinhomeNippeytest.sh: command not found
  • user11153
    user11153 about 9 years
    $(cygpath "%1") will not work with paths that contain a space. Use $(cygpath -ms "%1") instead.
  • Nippey
    Nippey about 9 years
    Nice finding. I won't pull it into my answer, because depending on how your HDD has been formatted, the old 8.3 format might be unsupported. Anyway: For me it works even without the -ms switch! This is my console then: Starting /bin/bash.exe >> xxx@xxx /cygdrive/c/Program Files I think it is a question of setting the right quotes.