# Microsoft batch file meets bash shellscript

Luca City, who already shared a nice readline keyboard shortcut with me, wrote me again on May 14 to share another unrelated, but very interesting trick:

Hi Rowan,
as you are interested in tricks and curiosities, I send you a thing.
I wanted a script to be runnable from both windows and linux and I found out a way to do it. Generally you can have two different files, one for each OS, but I started with this goal in mind and then it became a challenge. After trying a bit, playing with the strangest tricks of the two batch languages (bat and bash), I ended up with this solution. Actually it is not so useful 🙂 but anyway…

Well Luca, regardless of the usefulness of your script, I happen to think that it’s pure genius, so I’m going to share it here:

echo off
echo ; set +v # > NUL
echo ; function GOTO { true; } # > NUL

GOTO WIN
# bash part, replace it to suit your needs
exit 0

:WIN
REM win part, replace it to suit your needs

Give the script a .bat extension for Windows and set the executable bit(s) for Unix.

Thanks, Luca, for sharing another nice trick with us.

1. Comment by Lord Rybec
On July 10, 2012 at 16:24

This may not seem so useful at first glance, but I have found it quite useful. Where I work, I am setting up a system that requires some client side software to be useful. The client side portion is regularly updated, so it would be very difficult to make sure that the 6 Windows computers we have all stay up to date individually, in addition to the Mac and Linux laptops that some of our employees use. My solution was to put the Java client program on the network drive, so that all of the computers have access. This allows me to replace the single file instead of one per machine.

So, where the polyglot comes in is that the client software needs to run with some special switches. Also, I have to instruct our staff on how to use the software. My solution was to write a script that can run directly from the network drive, that includes all of the required switches. I also made a script, that runs the script on the network drive, that each user can copy to their local machine for a few user specific requirements. Since we have staff using Windows, Mac, and Linux, I needed a solution that would work for all three. Since I also have to write an instructional document, and do not want to write a section for each OS, using the same script file for all three OSs will save me a lot of work. So, I used the above polyglot to make both scripts runnable in Windows, Mac, and Linux (since OSX uses bash, the bash script covers both Mac and Linux).

Thank you for posting this. It has saved me a lot of time.

2. Comment by Rowan Rodrik
On November 23, 2012 at 16:43

Lord Rybec, Thanks for sharing your use case here. I’ve signalled Luca City, the author of the snippet, so that he too can enjoy your enthausiasm. 🙂