vincentf
vincentf

Reputation: 1499

How do I modify a shell environment variable in swift?

How can I access one of the shell env vars when using Process? If I use environment to set them, it will change all of the env vars.

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

Upvotes: 7

Views: 3815

Answers (2)

Nikolay Suvandzhiev
Nikolay Suvandzhiev

Reputation: 9075

If you're here searching for how to modify ProcessInfo.processInfo.environment, it is via a global function:

setenv("key", "value", 1)

(1 means "overwrite")

Upvotes: 2

mugx
mugx

Reputation: 10105

You might solve it appending on ProcessInfo.processInfo.environment (the inherited environment) your custom path (or whatever you need):

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

Upvotes: 10

Related Questions