Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"]
like image 634
vincentf Avatar asked Jan 29 '18 07:01

vincentf


People also ask

How do I change environment variables in shell?

You can set your own variables at the command line per session, or make them permanent by placing them into the ~/. bashrc file, ~/. profile , or whichever startup file you use for your default shell. On the command line, enter your environment variable and its value as you did earlier when changing the PATH variable.

How do I set an environment variable in Swift?

Use the jump bar next to the Run and Stop buttons in the project window toolbar to open the scheme editor. Select the Run step on the left side of the scheme editor. Click the Arguments button at the top of the scheme editor to access the environment variables. Use the Add (+) button to add the environment variables.

Can environment variables be changed?

You can edit other environment variables by highlighting the variable in the System variables section and clicking Edit. If you need to create a new environment variable, click New, and enter the variable name and value. To view and set the path through the Windows command line, use the path command.

What is the environment variable for the shell value?

In the C shell, a set of these shell variables have a special relationship to a corresponding set of environment variables. These shell variables are user, term, home, and path. The value of the environment variable counterpart is initially used to set the shell variable.


1 Answers

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 ?? "")
like image 139
Andrea Mugnaini Avatar answered Oct 03 '22 20:10

Andrea Mugnaini