How to get environment of a "fresh" login shell?

Hello fellow *nix users!

I am a bit confused how could I get an environment of a "fresh/clean" login shell, that is, the environment at that moment when user has started e.g. a new terminal/console or so.

So this is the sequence of actions I should be able to do in a single shell session:

  1. open a new terminal
  2. source a script which will set my environment based on some rule set
  3. do my tasks in that environment
  4. clean the environment from exports made in step #2
  5. source a new script which will do some other environment exports based on some other rule set
  6. do some stuff...
    And this sequence goes on and on...

Currently, there are two ways of doing this:
a) start a new terminal each time a new environment should be taken into use
b) before sourcing, start a new shell, e.g. bash/csh/what ever, and then do the sourcing... And when done -> exit that shell to get back the login shell and then start a new shell again

The problem with both ways mentioned above is lazy users. They would like to source the script setting the environment and after finished with that, source a new environment setting script. So they don't want to start a new shell or open up a new terminal.

So this is why I'm looking for a solution how to get an environment of a clean login shell. I don't think (might be wrong here..) there is a way to do something like:

bash -clean_shell_please "env > /tmp/some_file.txt"

Rsh does not help either as it seems to inherit its parent's environment as well. Rlogin seems like a good option BUT how can I get the environment via rlogin without user interaction, e.g. somehow piping commands to rlogin session?

Any ideas are highly appreciated! Thanks in advance!

-Miikka

I assume you mean "clean" as in zero environment variables.

One way

env - /usr/bin/ksh

Another way

set | awk '{ print "unset", $1 }' > ./tmp.tmp
. ./tmp.tmp

Now you have to source /etc/profile and then source ~/.profile or .bashrc or /home/username/.cshrc or whatever.

Thanks Jim for your quick reply.

With "clean" I meant the environment user gets when he opens up a new terminal and types env.

The first way you suggested + sourcing /etc/profile and ~/.profile does not result to same environment compared to new terminal session. I also get warnings "WARNING: terminal is not fully functional" after that.

The second way, using awk one liner, leads to a file full of "unset $1", i.e. $1 does not get evaluated...

You can start your scripts with this sequence:

REgards

You are correct, however, that is how its currently done as I mentioned in my first post:

The problem is that users would like to source those environment setting scripts directly from a shell without "start a new shell - source a script - do some stuff - exit the shell - start a new shell" cycle. So they just want to "source a script - do some stuff - source a new script - do some other stuff".

So what I'm trying to accomplish here is some sort of way to make those environment setting scripts to reset the environment back to what it was at the time of login and then applying new settings. One thing to notice also is that I cannot force users to modify their .profile to save the environment at login time.

I was hoping there would be some kind of way to use rlogin (or something else giving the same result) to get a clean login shell so that I could do the following in the beginning of each script setting an environment:

  • rlogin to a new shell which does not inherit the environment of parent
  • at rlogin shell: env > clean_env.txt
  • at script which is sourced:
    ** remove all environment variables and source clean_env.txt
    ** add some new environment variables on top of that

I know I could do this with Java or C, but I really would like to stick with shell script/perl/awk...

Sorry if I haven't been clear enough with my question :slight_smile:

Opening a new terminal will not always give you the same environment; it will inherit whatever environment is current.

What do you really want? And why?

I fixed the awk script - my error. However I'm with cfa- it is not really clear what you want let alone how to do it.