[SOLVED] How to clean the shell environment (variables and path) to original login state?
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
How to clean the shell environment (variables and path) to original login state?
Hello,
Is there a clean way to remove all post-logon, custom-added environment parameters (variables, paths, etc) from shell?
When I SSH to a cluster, my shell (bash) already has some system-generated environment parameters (lets call this env0). Then I source some paths, export some new variables, change few things in LD_LIBRARY_PATH, etc. Lets call the this shell environment after all these modifications env1.
Is there a clean way to revert back to env0 from env1 without logging out and logging back in?
I could write a script that will save the output of the <env> command to a file at the beginning of log on and then when I want to revert back, I can compare current environment with this saved file and modify the current environment to match with the saved environment. But I was wondering if there is a better way.
The reason I want to do this: I need to submit jobs on a cluster using SLURM. The sbatch command propagates all current environment variables to the compute nodes. I may have modified several environment parameters while preparing the job and I do not want those changes to be propagated to the compute node via the sbatch command.
You can source your original .bashrc to restore variables to their native login points, however newly defined variables in the shell will still exist. Whatever process you use to set any new variables, you can have a script or other manner to unset those, and there is a bash unset command which I've never used.
Otherwise the tried and true brute force method: Write a script to save env0, set env1 and then also support restoring env0.
You can source your original .bashrc to restore variables to their native login points, however newly defined variables in the shell will still exist. Whatever process you use to set any new variables, you can have a script or other manner to unset those, and there is a bash unset command which I've never used.
Otherwise the tried and true brute force method: Write a script to save env0, set env1 and then also support restoring env0.
Thanks rtmistler. I do want to get rid of the newly defined variables and newly appended parts in $PATH and $LD_LIBRARY_PATH.
Right now, I do use a script that compares env0 and env1 and resets old variables/paths and unsets new variables (using the bash unset command). I was wondering if there is a better way of doing so.
login(1) typically also sets: $SHELL $MAIL and $PATH (though this last one is usually overwritten by the contents of /etc/profile) so you may also want to add those to the list that you explicitly set.
login(1) typically also sets: $SHELL $MAIL and $PATH (though this last one is usually overwritten by the contents of /etc/profile) so you may also want to add those to the list that you explicitly set.
Thanks, GazL! This is is exactly what I was looking for.
In which case you should bump the rep (click "Yes" for "Did you find this post helpful").
He already did but he gave rep directly using the rep icon so it didn't increase the posts helpful counter. I was happy enough with a simple "thank you". I wouldn't notice another little green square if I got one.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.