Unix/Linux Go Back    


UNIX for Beginners Questions & Answers If you're not sure where to post a Unix or Linux question, post it here. All unix and Linux beginners welcome in this forum!

How are environment variables defined in a Gnome terminal session?

UNIX for Beginners Questions & Answers


Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 12-21-2017   -   Original Discussion by bodisha
bodisha's Unix or Linux Image
bodisha bodisha is offline
Registered User
 
Join Date: Jul 2008
Last Activity: 12 March 2018, 3:56 PM EDT
Posts: 21
Thanks: 7
Thanked 0 Times in 0 Posts
How are environment variables defined in a Gnome terminal session?

Hello... and thanks in advance for any help anyone can offer me with my question! I'm hoping someone will have a little patience with me and walk me through this!

I'm trying to understand a user login process on Centos 7 and I've gotten a bit confused trying to figure out when/how a Gnome terminal session defines environment variables (Gnome 3.14.4).

As near as I can tell.... a Gnome terminal will only define environment variables from the /etc/profile, ~/.profile, or ~/.bash_profile if the variables were exported in those scripts.

If they aren't exported then they don't appear in the output of either the set or printenv commands. The /etc/environment file is the exception (but it's not a shell login script).

The only thing I've found that looks like it would do this is the /etc/X11/xinit/xinitrc-common file when it sources the ~/.profile login script... But that's just a single login script and doesn't explain why exported variables in /etc/profile & ~!/.bash_profile are being displayed in set.




Code:
 [-r $HOME/.profile ] && . $HOME/.profile


If this reads the variables in the ~/.profile script... How are the variables being read for /etc/profile & ~/.bash_profile? If it matters.... I haven't selected "run command as login script"

Once again... thanks to anyone willing to read this and set me straight!
Sponsored Links
    #2  
Old Unix and Linux 12-21-2017   -   Original Discussion by bodisha
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is online now Forum Staff  
Administrator
 
Join Date: Jul 2012
Last Activity: 20 April 2018, 6:02 PM EDT
Location: San Jose, CA, USA
Posts: 11,207
Thanks: 633
Thanked 3,903 Times in 3,341 Posts
Gnome terminal is not a shell and doesn't really set environment variables for you. Somewhere in your setup for Gnome terminal you told it that you wanted it to start a shell for you. It starts that shell as a login shell which causes it to read its own initialization scripts.

If one of the scripts your login shell is reading is ~/.bash_profile, the shell that has been configured for you is bash.

If you were using a BSD, Linux, or UNIX system the login shell for a user would be found in the user database (frequently contained in a file named /etc/passwd). I believe that Windows has a similar user database, but I don't remember where it is located nor how it is accessed.
Sponsored Links
    #3  
Old Unix and Linux 12-21-2017   -   Original Discussion by bodisha
bodisha's Unix or Linux Image
bodisha bodisha is offline
Registered User
 
Join Date: Jul 2008
Last Activity: 12 March 2018, 3:56 PM EDT
Posts: 21
Thanks: 7
Thanked 0 Times in 0 Posts
First... thanks for the reply! I hope I can ask for your patience as I try to understand this

I've done quite a bit of reading to try and understand this.... The documentation I had read said that the Bash login scripts (/etc/profile, ~/.bash_profile, ~/.profile, etc) wouldn't get sourced/executed in an interactive non-login shell mode (i.e. Gnome terminal)... and this specific shell mode would only read the /etc/bashrc and the ~/.bashrc scripts.

This had me slightly confused on how a Gnome terminal might go about getting environment variables that would be required to make programs work correctly in an interactive login shell mode (i.e. SSH, Telnet, etc). So I started playing with it.

Like I said, I discovered, if I defined variables simply as local variables (without the 'export' command) in the login scripts they would not be displayed in the output of set/printenv commands.... Which is how the documentation said it would behave

But by playing with things, I discovered if I exported the variable in the Bash login scripts, they were appearing in the output of the set/printenv commands. Initially I thought they were being exported as a global variable from an SSH (putty) sessions

So I did a reboot, and logged in directly to Gnome (Multiple times), to make sure I wasn't accidentally exporting a global variable by a SSH session.

To try and figure this out... I started poking around Gnome and found the /etc/X11/xinit/xinitrc-common file sources the $HOME/.profile script. But this hardly explains the exported variables from the other login scripts... So I'm left pretty baffled on why documentation is saying one thing... But what I'm see Centos & Gnome do is something totally different

Anyway... once again thank you for your reply... If you could help me understand this I would be extremely grateful!!!
    #4  
Old Unix and Linux 12-21-2017   -   Original Discussion by bodisha
gull04's Unix or Linux Image
gull04 gull04 is offline Forum Advisor  
Registered User
 
Join Date: Dec 2004
Last Activity: 19 April 2018, 3:37 AM EDT
Location: Isle-of-Skye
Posts: 758
Thanks: 19
Thanked 140 Times in 123 Posts
Hi Bodisha,

You have actually answered the question yourself, when you say "Gnome Terminal" - you can in fact drop the word Gnome. Its just a terminal and will read the appropriate file on start up.

It doesn't matter what desktop environment you use, when you start the terminal it will read the required initialisation files and will have the variables set as determined by the file contents.

Regards

Gull04
Sponsored Links
    #5  
Old Unix and Linux 12-21-2017   -   Original Discussion by bodisha
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is online now Forum Staff  
Administrator
 
Join Date: Jul 2012
Last Activity: 20 April 2018, 6:02 PM EDT
Location: San Jose, CA, USA
Posts: 11,207
Thanks: 633
Thanked 3,903 Times in 3,341 Posts
In a macOS version 10.13.2 terminal window, if I type in the command:


Code:
who am i

it will reply with something like:


Code:
dwc      ttys006  Dec 17 12:52

If I then type in the command:


Code:
ps -t ttys006

it gives me something like:


Code:
  PID TTY           TIME CMD
  849 ttys006    0:00.02 login -pf dwc /bin/ksh
  850 ttys006    0:01.72 -ksh
81761 ttys006    0:00.00 ps -t ttys006

I would expect that running similar commands in your Gnome terminal window will produce similar results, but the first field in the output from who am i will be your login ID instead of mine and the 2nd field will be your terminal device ID instead of ttys006. If you then issue that ps command with your terminal device ID as the -t option option-argument, I would expect to see similar output on your screen with the -ksh in my output replaced by a -bash in your output. The login command shown in my output is what macOS uses to start a login session. There might or might not be a similar line in your ps output depending on how Centos starts a login session.
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Doing math using user defined input and system environment variables saravanapandi Shell Programming and Scripting 3 01-31-2014 11:46 PM
Finding WHERE an environment variable is defined. dissectcode UNIX for Dummies Questions & Answers 6 04-18-2011 07:59 PM
I am not able to login in gnome session and java session in Sun solaris 9& 10 durgaprasadr13 Solaris 0 12-10-2008 08:13 AM
sqlplus session being able to see unix variables session within a script 435 Gavea Shell Programming and Scripting 2 07-03-2006 10:11 AM
environment variable is not defined alien12 Linux 0 02-09-2006 10:12 PM



All times are GMT -4. The time now is 06:12 PM.