Coming Up for Air

Executable Kotlin Scripts

Monday, December 23, 2019 |

A user in #kotlin on Freenode asked how to run a Kotlin script. While the Kotlin docs are pretty clear on how to do that, I thought I’d make a quick post to show how to make an easily executable Kotlin script.

The first step is to name the file with a .kts extension:

test.kts
1
println("Hello, world!")

You can then run it like this:

1
2
$ kotlinc -script test.kts
Hello, world!

That’s pretty cool (and pretty quick on my laptop), but the command line is a bit cumbersome. Let’s fix that with two small changes. First, let’s add a shebang:

test.kts
1
2
#!/usr/bin/env -S kotlinc -script
println("Hello, world!")

Then we set the executable bit and run it:

1
2
3
$ chmod +x test.kts
$ ./test.kts
Hello, world!

Easy peasy. Now you a more powerful language in your shell scripting toolbox. Enjoy!

Search

    Quotes

    Sample quote

    Quote source

    About

    My name is Jason Lee. I am a software developer living in the middle of Oklahoma. I’ve been a professional developer since 1997, using a variety of languages, including Java, Javascript, PHP, Python, Delphi, and even a bit of C#. I currently work for Red Hat on the WildFly/EAP team, where, among other things, I maintain integrations for some MicroProfile specs, OpenTelemetry, Micrometer, Jakarta Faces, and Bean Validation. (Full resume here. LinkedIn profile)

    I am the president of the Oklahoma City JUG, and an occasional speaker at the JUG and a variety of technical conferences.

    On the personal side, I’m active in my church, and enjoy bass guitar, running, fishing, and a variety of martial arts. I’m also married to a beautiful woman, and have two boys, who, thankfully, look like their mother.

    My Links

    Publications