Is it possible to recover the original environment that a shell script was invoked with? I'm not trying to write a program that depends on the ability to access the original environment, I'm wondering if it's guaranteed to be inaccessible.
I have a simple shell script that creates a new environment variable and changes the value of PATH.
#!/bin/sh
PATH=/usr/bin
NEW_VAR=new_var_value; export NEW_VAR
env
Is there a way to recover the original value of PATH or a way to tell that NEW_VAR was not originally present when the shell script was executed?
I know that argv, envp and the path to the executable are all set early on in the execv*(2) system call, but I'm not sure "where" or "how" they're stored. My guess is that the ability to manipulate the argument vector using shift and the environment using export, unexport, and assignments doesn't actually change the what the argv and envp are fundamentally set to. There's just logic inside the shell to make sure that new calls to execv*(2) use the right envp when calling another command (e.g. some-command | LC_ALL=C sort).
xargs -0 -L 1 < /proc/$$/environto see your original environment.