How to run shell files FROM WITHin visual studio

I had a problem recently : my scripting experience coming mostly from Linux, I’m much more used to shell scripts than .bat scripts. And despite working on Windows, I still prefer using shell script language mostly for human factors reasons (I find batch syntax ugly and frankly impossible to understand and/or remember).

But contrary to the Linux world, a shell executable (also known as sh.exe, or any other flavor like Bash or Zsh to name a few) is not installed by default on a Windows. You have to install it yourself and do a bunch of wiring so that shell files are automatically executed by the shell executable using some kind of fake Unix environment (Cygwin is a good example).

My problem of the day is one of yet another kind…

I have a shell script generating a list of source files that will be fed to a CMake script. I’m working with a C++ project inside the Visual Studio IDE.

So basically what I want to do is a simple way to execute a shell script file from within Visual Studio with as least friction as possible. Double-clicking on a .bat file inside VS automatically runs this batch file, and I typically want to achieve the same behavior, but for a .sh file. Turns out after reading quite a bit of Stack overflow, I may have arrived to a satisfactory solution !

This relies heavily on the fact that you probably have already installed something like Git Bash for Windows on your machine that actually has installed the shell interpreter (sh.exe) for you. This makes everything easier and is so common that I think it’s a pretty safe prerequisite to assume.

In this case it’s simple : add your .sh script file to your Visual Studio solution one way or another, then right-click on it in the Solution Explorer, and select « Open With… ». This will determine what program gets called when you double click on the file in this view.

Then in the opened pop-up, all is left to do is to click on Add to add the shell interpreter to the list of available programs. Once you did it, in the new dialog click on the « … » button to browse to where sh.exe is located. On my system (a regular Windows 10 Git for Windows install), it’s under C:\Program Files\Git\bin for example.

Next, close the dialog, but don’t forget to select it and click on « Set as Default » so Visual Studio knows that’s the program to run every time you double click on that file.

Laisser un commentaire

Votre adresse de messagerie ne sera pas publiée.