Set shell environment variables from within Golang script?

1,241 views
Skip to first unread message

Victor Hooi

unread,
Jun 12, 2014, 1:37:42 AM6/12/14
to golan...@googlegroups.com
Hi,

Is there a way to edit the user's environment variables from within a Go script, so that they persist when the Go program finishes execution?

We're using this to modify the user's PATH variable to control which version of a program is being run. (Alternatives would be messing around with symlinks or just mv-ing files around, however, the PATH seems like a lighter and less messy way of doing it).

Is this possible from Golang at all?

Cheers,
Victor

Rui Ueyama

unread,
Jun 12, 2014, 1:46:01 AM6/12/14
to Victor Hooi, golang-nuts
That's not doable in any programming language because there's no way to change parent's environment variable from child process.

You can print out something like this from a command

  export PATH=/foo/bar:$PATH

and then evaluate the output in a shell using `` to change the shell's environment variable, but that's different from changing parent's environment from child.


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages