Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

277
Views
Read environment variable in Kotlin/Native

OK, so I want to create a native app to read some environment variables, and based on some business logic, change the value of them to something else. I decided to do it in Kotlin.

I managed to find out how to change the system environment variable, by creating the functions:

fun call(arg: String) = platform.posix.system(command)

fun setEnvironmentVariable(variable: String, value: String) {
    println("Changing value of $variable to $value")
    call("SETX $variable $value")
}

But it only changes the value. I would like to store it in a Kotlin variable and do some manipulations on it.

Issuing the statement call("SET $variable") prints the value to the command prompt, but I cant figure out how to capture it from Kotlin. I guess if there was a way to extract the output from command prompt to a String that would make it easier, even though Windows outputs it on the form myvariable=myvalue instead of just the value.

Thanks in advance

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Here's how to read an environment variable in Kotlin/Native:

import platform.posix.*
import kotlinx.cinterop.*

fun main() {
    val variable = "whatever..."
    println(getenv(variable)?.toKString())
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!