# Executing Kotlin scripts in command shell

**URL:** <https://discuss.kotlinlang.org/t/executing-kotlin-scripts-in-command-shell/1926>\
**Category:** Support\
**Created:** [August 17, 2016, 3:59pm UTC](https://discuss.kotlinlang.org/t/executing-kotlin-scripts-in-command-shell/1926 "2016-08-17T15:59:51Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![dzoettl](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/dzoettl/32/2082_2.png) [@dzoettl](https://discuss.kotlinlang.org/u/dzoettl)\
**Post date:** [August 17, 2016, 3:59pm UTC](https://discuss.kotlinlang.org/t/executing-kotlin-scripts-in-command-shell/1926/1 "2016-08-17T15:59:51Z")

</div>

Hi,

I would like to replace a bash script with a kotlin script.  
Therefore Shebang support [Wikipedia shebang](https://en.wikipedia.org/wiki/Shebang_(Unix))  
would be helpful.

I noticed that you can add a shebang line to a kotlin script, but executing it gives some error message.

Ok: `~/kotlinc/bin/kotlinc -script list_folders.kts .`  
but: `~/kotlinc/bin/kotlin list_folders.kts .`  
`error: running Kotlin scripts is not yet supported`

I would like to use:  
`ln -s ~/kotlinc/bin/kotlin /usr/local/bin/kotlin`  
`./list_folders.kts .`

when adding `#!/usr/local/bin/kotlin` to the script file.

Sincerely  
Dirk

---

<div class="post-metadata">

**Author:** ![udalov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/udalov/32/5679_2.png) [@udalov](https://discuss.kotlinlang.org/u/udalov)\
**Post date:** [August 17, 2016, 4:25pm UTC](https://discuss.kotlinlang.org/t/executing-kotlin-scripts-in-command-shell/1926/2 "2016-08-17T16:25:58Z")

</div>

Currently the only way to run a Kotlin script from the command line is:

```
kotlinc -script file.kts <arguments>

```

The `kotlin` command is going to support running scripts at some point, but as the error message says, it’s not yet done.

---

<div class="post-metadata">

**Author:** ![molchanoviv](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/molchanoviv/32/1846_2.png) [@molchanoviv](https://discuss.kotlinlang.org/u/molchanoviv)\
**Post date:** [August 17, 2016, 6:25pm UTC](https://discuss.kotlinlang.org/t/executing-kotlin-scripts-in-command-shell/1926/3 "2016-08-17T18:25:27Z")

</div>

As a temporary solution you can use kscript [GitHub - kscripting/kscript: Scripting enhancements for Kotlin](https://github.com/holgerbrandl/kscript)

---

<div class="post-metadata">

**Author:** ![Atlan](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/atlan/32/4176_2.png) [@Atlan](https://discuss.kotlinlang.org/u/Atlan)\
**Post date:** [October 4, 2017, 1:06pm UTC](https://discuss.kotlinlang.org/t/executing-kotlin-scripts-in-command-shell/1926/4 "2017-10-04T13:06:59Z")

</div>

This is what I did:

```
#!/usr/bin/env kotlinc -script

import java.io.File

val folders: Array<out File> = File(args[0]).listFiles { file -> file.isDirectory }!!

folders.forEach { folder -> println(folder) }

```

---

<div class="post-metadata">

**Author:** ![dlee](https://avatars.discourse-cdn.com/v4/letter/d/d6d6ee/32.png) [@dlee](https://discuss.kotlinlang.org/u/dlee)\
**Post date:** [October 6, 2017, 2:29pm UTC](https://discuss.kotlinlang.org/t/executing-kotlin-scripts-in-command-shell/1926/5 "2017-10-06T14:29:03Z")

</div>

I’m not associated with kscript, but after trying it I want to post that I recommend taking a look.  
I was skeptical after looking at the implementation ( a fairly complex looking shell script) that it would be any  
better, especially performance, then Atlan’s suggestion (which works fine and does with in simplicity points).  
But a simple ‘1 liner’ test (and then an hour later not-so-simple because I didn’t believe it) –

Put the above in a file ( I had to change to ‘#!/my/path/to/kotlinc -script’) /tmp/file.kts

then try a simple test:

time /tmp/file.kts

Run that a few times

Then delete the first line (the shebang) and try again with kscript  
For me - the very first run of kscripts was infinitesimally longer (added about .001s)  
But after that 10-40x shorter every run (depends on script complexity and what its doing).  
Using kotlinc directly makes no measurable difference.

```
kotlinc:

real 0m2.074s
user 0m4.377s
sys 0m0.133s

kscript

real 0m0.166s
user 0m0.145s
sys 0m0.066s

```

* * *

Note the user time – 4.377 sec for kotlnc, .145 for kscript. \> 30x diff

That’s why I didn’t believe it 🙂 kscript is a 1 file shell script – its not doing anything really fancy or complicated - nothing you can’t do yourself or do without - except convenience … but I doubt I could do better easily so Im not going to bother.

Leaving the ‘pre-compiled’ script cached is a huge win – if you are using a script more than once. If you are not, its still a big win because the performance and simplicity is equivalent but you don’t have to do things differently each time.  
JVM startup time is bad enough that it makes JVM CLI apps non-performant in many scripting use cases,  
add to that the compile startup time and now looking at 2+ maybe FIVE secs per invocation – that pushes it over the line  
for ‘no brainer’ script replacements – put that in a for loop or multiple $(script) calls and your simple scripts take minutes instead of seconds or less. Dropping that down to a .1s appx startup overhead brings it back to the realm of reasonable ‘no brainer’ script use cases.

Maybe someday kotlin will optimize the startup - until then, I’m going with kscript  
-David

---

<div class="post-metadata">

**Author:** ![Christian.Bongiorno](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/christian.bongiorno/32/2687_2.png) [@Christian.Bongiorno](https://discuss.kotlinlang.org/u/Christian.Bongiorno)\
**Post date:** [November 15, 2018, 5:55am UTC](https://discuss.kotlinlang.org/t/executing-kotlin-scripts-in-command-shell/1926/6 "2018-11-15T05:55:17Z")

</div>

Any updates on this?

---

<div class="post-metadata">

**Author:** ![dlee](https://avatars.discourse-cdn.com/v4/letter/d/d6d6ee/32.png) [@dlee](https://discuss.kotlinlang.org/u/dlee)\
**Post date:** [November 30, 2018, 2:34am UTC](https://discuss.kotlinlang.org/t/executing-kotlin-scripts-in-command-shell/1926/7 "2018-11-30T02:34:49Z")

</div>

kscript is better then ever.  
kotlinc still works, and under the hood kscript calls it – but kscript manages a lot of tedious cache logic and has evolved to have a lot more features – they are _nearly identical_ to the scripting features in IntelliJ IDE – but I dont know of any cli which implements those.

---

<div class="post-metadata">

**Author:** ![Wavesonics](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/wavesonics/32/2749_2.png) [@Wavesonics](https://discuss.kotlinlang.org/u/Wavesonics)\
**Post date:** [August 25, 2019, 7:18pm UTC](https://discuss.kotlinlang.org/t/executing-kotlin-scripts-in-command-shell/1926/8 "2019-08-25T19:18:47Z")

</div>

Did this not give you an error?  
`error: unable to instantiate class test.Test (test.kts): java.lang.NoClassDefFoundError: kotlin/script/templates/standard/ScriptTemplateWithArgs`
