environment-variableskotlin

How to read an environment variable in Kotlin?


I'd like to get a certain value from an environment variable in my Kotlin app, but I can't find anything about reading environment variables in the core libraries documentation.

I'd expect it to be under kotlin.system but there's really not that much there.


Solution

  • It is really easy to get a environment value if it exists or a default value by using the elvis operator in kotlin:

    val envVar: String = System.getenv("varname") ?: "default_value"