I'm developing console app for mac os in swift and i want to access user environment variables when calling bash command. I use the following code to run bash command in swift:
@discardableResult
func shell(_ command: String) -> String? {
let task = Process()
task.launchPath = "/usr/bin/env"
task.arguments = ["bash", "-c", command]
let pipe = Pipe()
task.standardOutput = pipe
task.launch()
let data = pipe.fileHandleForReading.readDataToEndOfFile()
let output = String(data: data, encoding: String.Encoding.utf8)
return output
}
When i run /usr/bin/env in my terminal i see the desired variable but when i run shell("/usr/bin/env") in code i don't see one. The ProcessInfo.processInfo.environment["MY_ENV_VAR"] also don't show me the needed variable.
How to access user defined env vars in swift?
