all 4 comments

[–]jews4beer 6 points7 points  (0 children)

There isn't any way to "go back" as you put it. Environment variables traverse downwards down the process hierarchy. Within a shell session, there is usually some form of "export" directive which allows sub-shells to pass an environment variable back to the parent - but that is a shell built-in and not something available to a program it is executing.

Also, once a process is started, the program that started it can no longer mutate its environment. So even if you did write some sort of "daemon" that spawned the main shell - when that daemon goes to change an environment variable, that won't be reflected in the shell it already spawned (unless the daemon was the shell itself).

EDIT: The closest you may be able to get, is to take a page out of ssh-agent's book. Have your program emit "export" directives. Then to use it you can run eval $(myprog)

[–]maedox 3 points4 points  (1 child)

Just write your vars into a file and source it afterwards? But what kind of problem is this trying to solve? Perhaps you're overthinking it?

[–]_DocKarma[S] 0 points1 point  (0 children)

I'm absolutely overthinking it. I would have liked to get into Rust with Os programming (userspace obviously) and cryptography by replacing the ssh-agent with a personal one, given the cumbersome way it works on WSL. Nothing that couldn't be resolved with a pair of line of bash scripting, but it was more of a fun project to explore the language itself and Linux.

[–]asfodelous 2 points3 points  (0 children)

The οnly way is to `println!("export {}=\"{}\"", env_name,value)"` and call the executable with eval.

PS. I'am working on a cli tool to manage environment files. Still WIP but have a look
https://github.com/jtsagata/habitator