Log in

View Full Version : Cygwin to Run Shell Scripts on Windows


jay123210599
9th March 2025, 23:56
How do I use Cygwin to run MacOS/Linux shell scripts like these on Windows?

https://silvae86.github.io/2021/02/16/splitting-videos-for-google-photos/

LoRd_MuldeR
10th March 2025, 01:07
Just install Cygwin with the installer from the official web-site and then run "cygwin.bat" from Cygwin install directory to open a terminal:
https://www.cygwin.com/

But be aware: Even though Cygwin allows you to run shell scripts on Windows, and it provides most of the "standard" Unix tools (bash, make, grep, etc. pp.), not every Linux or MacOS script/command will "magically" just work 1:1 on Windows!

For example, "brew" (Homebrew) is a package manager for MacOS, so it won't work on Windows/Cygwin. If you want to install packages in Cygwin, just use Cygwin's package manager, i.e. Cygwin Setup ;)
https://www.cygwin.com/install.html

jay123210599
10th March 2025, 05:36
Just install Cygwin with the installer from the official web-site and then run "cygwin.bat" from Cygwin install directory to open a terminal:
https://www.cygwin.com/

But be aware: Even though Cygwin allows you to run shell scripts on Windows, and it provides most of the "standard" Unix tools (bash, make, grep, etc. pp.), not every Linux or MacOS script/command will "magically" just work 1:1 on Windows!

For example, "brew" (Homebrew) is a package manager for MacOS, so it won't work on Windows/Cygwin. If you want to install packages in Cygwin, just use Cygwin's package manager, i.e. Cygwin Setup ;)
https://www.cygwin.com/install.html

All right, I installed Cygwin, now do how I use it to run the script above so I can complete this task? https://forum.doom9.org/showthread.php?t=185986

Which file format/extension should I put the script in?

LoRd_MuldeR
10th March 2025, 21:14
All right, I installed Cygwin, now do how I use it to run the script above so I can complete this task? https://forum.doom9.org/showthread.php?t=185986

Which file format/extension should I put the script in?

Usually shell scripts are stored with a ".sh" file extension.

Once you have saved the script, run "cygwin.bat" from Cygwin install directory to open a terminal, then (in the terminal) "cd" to the directory where your script file is located, and then run "./my_script.sh".

Note that a Windows path like "C:\path\to\my_script.sh" would translate to "/cygdrive/c/path/to/my_script.sh" in Cygwin.

https://i.imgur.com/LjRFZsYl.png (https://i.imgur.com/LjRFZsY.png)

jay123210599
10th March 2025, 22:43
Usually shell scripts are stored with a ".sh" file extension.

Once you have saved the script, run "cygwin.bat" from Cygwin install directory to open a terminal, then (in the terminal) "cd" to the directory where your script file is located, and then run "./my_script.sh".

Note that a Windows path like "C:\path\to\my_script.sh" would translate to "/cygdrive/c/path/to/my_script.sh" in Cygwin.

https://i.imgur.com/LjRFZsYl.png (https://i.imgur.com/LjRFZsY.png)

Here is the file path of my script: "C:\Users\User\Downloads\my_script.sh"

What does that translate to? Is there a tool for this sort of thing? Also, do I add "./" at the beginning of it?

LoRd_MuldeR
11th March 2025, 00:48
Here is the file path of my script: "C:\Users\User\Downloads\my_script.sh"
What does that translate to?
/cygdrive/c/Users/User/Downloads/my_script.sh :confused:

Is there a tool for this sort of thing?
Yes, and it's called "cygpath" (https://i.imgur.com/gb7h5El.png) ;)

But you don't normally need that. It's not that complicated. All your Windows "drives" are mounted at "/cygdrive/x/" by default, with "x" being the corresponding Windows "drive letter".

So, your "C:" drive will be mounted at "/cygdrive/c/", your "D:" drive will be mounted at "/cygdrive/d/", and so on...

Also, do I add "./" at the beginning of it?
If you want to run an executable or script from the current directory (rather than specifying an absolute path), then yes, you have to put a "./" in front of it!

So, with your example, you can either run "cd /cygdrive/c/Users/User/Downloads" followed by "./my_script.sh", or you can just run "/cygdrive/c/Users/User/Downloads/my_script.sh".

jay123210599
11th March 2025, 00:55
If you want to run a script from the current directory (rather than specifying an absolute path), then yes, you have to put a "./" in front of it.

Like this?

.//cygdrive/c/Users/User/Downloads/my_script.sh

LoRd_MuldeR
11th March 2025, 01:02
Like this?

.//cygdrive/c/Users/User/Downloads/my_script.sh

Uhm, no :scared:

As said above, either you use the "cd" command to change the current directory to the directory where your script is located (e.g. "cd /cygdrive/c/Users/User/Downloads"), and then you run "./my_script.sh".

...or you enter the absolute path of the script that you want to run, which would be "/cygdrive/c/Users/User/Downloads/my_script.sh" in your case.


(As an aside: You can always run the "pwd" (https://i.imgur.com/dReUfTa.png) command to check in which directory you currently are!)