如何在swift中修改shell环境变量?

时间:2018-01-29 07:01:19

标签: swift macos process

使用Process时,如何访问其中一个shell env变量?如果我使用environment来设置它们,它将改变所有的env vars。

let task = Process()
// How do I modify PATH only instead of setting the whole dictionary
task.environment = ["PATH": "/usr/local/bin"]

1 个答案:

答案 0 :(得分:4)

您可以解决它附加在ProcessInfo.processInfo.environment(继承的环境)您的自定义路径(或您需要的任何内容):

let task = Process()
var environment =  ProcessInfo.processInfo.environment
environment["PATH"] = "/usr/local/bin"
task.environment = environment
print(task.environment ?? "")