Launching Renamed Kotlin Scripts

Recently I’ve been writing quite a few one off scripts using Kotlin’s scripting facilities. Some of these have become relatively complex but useful enough that I’d like to have them installed in a location on my path. I’ve discovered that when a Kotlin script no longer has a main.kts extension, Kotlin really struggles at figuring out how to execute the script.

So, without further ado, here’s the solution:

#!/usr/bin/env -S kotlin -howtorun .main.kts

Start your script with that magic line and it’ll launch correctly, even after you have renamed the script to not end with a .main.kts extension.