How to execute bash command in Kotlin

Viewed 12353

Sorry but I could not find the answer from googling...what is the syntax for running a bash command in kotlin? I would like to do a curl command. The documentation out there seems very limited or maybe I am bad at googling?

2 Answers

As curl is not a bash-specific application, you can just start it like any other process. Assuming you are using kotlin on the JVM:

val process = ProcessBuilder("curl", "https://example.com").start()
process.inputStream.reader(Charsets.UTF_8).use {
  println(it.readText())
}
process.waitFor(10, TimeUnit.SECONDS)

Instead of using curl you might want to look into kotlin or java libraries, depending on your problem it could be easier and even faster than starting the curl process.

Related