LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 03-10-2017, 11:36 AM   #1
someshpr
Member
 
Registered: Jul 2009
Location: WI, USA
Distribution: Debian 8, Ubuntu 16.04, CentOS 7
Posts: 143

Rep: Reputation: 28
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.
 
Old 03-10-2017, 12:54 PM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,314
Blog Entries: 13

Rep: Reputation: 4370Reputation: 4370Reputation: 4370Reputation: 4370Reputation: 4370Reputation: 4370Reputation: 4370Reputation: 4370Reputation: 4370Reputation: 4370Reputation: 4370
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.
 
Old 03-10-2017, 01:53 PM   #3
someshpr
Member
 
Registered: Jul 2009
Location: WI, USA
Distribution: Debian 8, Ubuntu 16.04, CentOS 7
Posts: 143

Original Poster
Rep: Reputation: 28
Quote:
Originally Posted by rtmistler View Post
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.
 
Old 03-11-2017, 06:16 AM   #4
GazL
LQ Veteran
 
Registered: May 2008
Posts: 5,917

Rep: Reputation: 3893Reputation: 3893Reputation: 3893Reputation: 3893Reputation: 3893Reputation: 3893Reputation: 3893Reputation: 3893Reputation: 3893Reputation: 3893Reputation: 3893
You could try something like this:

exec env -i LOGNAME="$LOGNAME" HOME="$HOME" TERM="$TERM" bash -l

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.
 
1 members found this post helpful.
Old 03-13-2017, 05:46 PM   #5
someshpr
Member
 
Registered: Jul 2009
Location: WI, USA
Distribution: Debian 8, Ubuntu 16.04, CentOS 7
Posts: 143

Original Poster
Rep: Reputation: 28
Quote:
Originally Posted by GazL View Post
You could try something like this:

exec env -i LOGNAME="$LOGNAME" HOME="$HOME" TERM="$TERM" bash -l

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.
 
1 members found this post helpful.
Old 03-13-2017, 05:53 PM   #6
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 19,695

Rep: Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547
In which case you should bump the rep (click "Yes" for "Did you find this post helpful").
 
Old 03-13-2017, 06:37 PM   #7
GazL
LQ Veteran
 
Registered: May 2008
Posts: 5,917

Rep: Reputation: 3893Reputation: 3893Reputation: 3893Reputation: 3893Reputation: 3893Reputation: 3893Reputation: 3893Reputation: 3893Reputation: 3893Reputation: 3893Reputation: 3893
Quote:
Originally Posted by syg00 View Post
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.
 
Old 03-13-2017, 06:50 PM   #8
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 19,695

Rep: Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547
reproof accepted.
 
  


Reply

Tags
bash, environment, slurm


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
set up path and environment variables in Ubuntu 14.04 for JSF jone kim Linux - Software 1 08-03-2014 12:20 PM
setting environment path variables manish jain Linux - Newbie 3 10-04-2007 11:21 AM
Need help w/ a few PATH and Environment variables DaFrEQ SUSE / openSUSE 3 06-10-2006 04:12 PM
How to set PATH and CLASSPATH environment variables bronko Linux - Newbie 2 12-20-2003 11:13 AM
rh7.1 environment variables (eg path) dave_lane Linux - Newbie 1 09-20-2001 08:05 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:36 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration