LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 07-28-2011, 09:58 PM   #1
greenpool
Member
 
Registered: Sep 2010
Posts: 41

Rep: Reputation: 0
Ubuntu etc/profile: environmental variable doesn't display content


Hi,

i am trying to declare an environmental variable in the /etc/profile' (as per the tutorial i'm following) but when i declare it and do an echo i get nothing.

Here's what i've done so far..

inside /etc/profile:

# /etc/profile: system-wide .profile file for the Bourne shell (sh(1))
# and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).

if [ -d /etc/profile.d ]; then
for i in /etc/profile.d/*.sh; do
if [ -r $i ]; then
. $i
fi
done
unset i
fi

if [ "$PS1" ]; then
if [ "$BASH" ]; then
PS1='\u@\h:\w\$ '
if [ -f /etc/bash.bashrc ]; then
. /etc/bash.bashrc
fi
else
if [ "`id -u`" -eq 0 ]; then
PS1='# '
else
PS1='$ '
fi
fi
fi


LINUXTEST=Scripting
export LINUXTEST


umask 022

********************

I found it odd that there were no other variables declared inside the /etc/profile.

anyway in the i did
Code:
echo $LINUXTEST
which does nothing. although i was able to declare this within the shell and it printed the contents


on a side note, i've also created a new user and i was wondering how do you switch between users via the shell? thanks!
 
Old 07-29-2011, 01:25 AM   #2
A.Thyssen
Member
 
Registered: May 2006
Location: Brisbane, Australia
Distribution: linux
Posts: 158

Rep: Reputation: 44
That is because they are typically declared in individual profile files in /etc/profile.d subdirectory, typically for specific software packages.

The /etc directory is usually used for system wide environemtn setup for ALL users.

For individual users, environment variables are declared in individuals ~/.profile and ~/.bashrc files. Which if you look at actually source the /etc/profile so as to first get the system wide settings.


for some specific software would create a file /etc/profile.d/{software}.sh
and put that software settings in that file.

More importantly LOG and document what changes you made to the system, so when 3 years later you re-install your system you know what changes you made on your old system!
 
Old 07-29-2011, 02:11 AM   #3
greenpool
Member
 
Registered: Sep 2010
Posts: 41

Original Poster
Rep: Reputation: 0
added env variable to ~/.profile still doesn't work

Quote:
Originally Posted by A.Thyssen View Post
That is because they are typically declared in individual profile files in /etc/profile.d subdirectory, typically for specific software packages.

The /etc directory is usually used for system wide environemtn setup for ALL users.

For individual users, environment variables are declared in individuals ~/.profile and ~/.bashrc files. Which if you look at actually source the /etc/profile so as to first get the system wide settings.


for some specific software would create a file /etc/profile.d/{software}.sh
and put that software settings in that file.

More importantly LOG and document what changes you made to the system, so when 3 years later you re-install your system you know what changes you made on your old system!

i tried adding the environment variable to ~./profle and i'm still getting no output. i can't seem to figure out what i'm doing wrong...any thoughts?? thanks!
 
Old 07-29-2011, 02:54 AM   #4
akuma_linux
Member
 
Registered: Aug 2006
Distribution: Ubuntu, CentOS
Posts: 31

Rep: Reputation: 15
Hi greenpool,

Try adding the environment variables into /etc/environment
 
Old 07-29-2011, 03:20 AM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by A.Thyssen View Post
For individual users, environment variables are declared in individuals ~/.profile and ~/.bashrc files. Which if you look at actually source the /etc/profile so as to first get the system wide settings.
They could be written that way but it is not necessary; bash itself sources /etc/profile when the new shell is a login shell as described here.

For users who have bash in their /etc/passwd entry ...

All user processes inherit variables exported in /etc/profile this way. This includes GUI processes because GUI logon begins with a bash logon so every GUI process is a child/descendant of the bash logon process. A process in that tree could unset such environment variables in which case its descendants would not have the unset environment variables.

A possible gotcha is that terminals started in the GUI environment are not logon shells so will not have any environment variables set by changes in /etc/profile since logon (unless the interactive, non-logon bash initialisation file, usually ~/.bashrc, does source /etc/profile).
 
Old 07-30-2011, 06:17 PM   #6
greenpool
Member
 
Registered: Sep 2010
Posts: 41

Original Poster
Rep: Reputation: 0
Thanks for the info guys.

I shall play around and see if it works!
 
Old 07-30-2011, 06:24 PM   #7
rkski
Member
 
Registered: Jan 2009
Location: Canada
Distribution: CentOS 6.3, Fedora 17
Posts: 247

Rep: Reputation: 51
Put it in ~/.bashrc and make sure to start a new shell before trying to 'echo'.
 
Old 07-30-2011, 07:06 PM   #8
greenpool
Member
 
Registered: Sep 2010
Posts: 41

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by rkski View Post
Put it in ~/.bashrc and make sure to start a new shell before trying to 'echo'.
I thought System wide environment variables goes into ~/.profile or ~/.bash_profile and functions and aliases go into ~/.bashrc?
 
Old 07-30-2011, 07:12 PM   #9
greenpool
Member
 
Registered: Sep 2010
Posts: 41

Original Poster
Rep: Reputation: 0
So i managed to get my original question working but now i'm facing another dilemma

i have 2 users
test
hnz

PROBLEM 1

in test /etc/profile environment variable is defined as

Code:
LINUXTEST=Scripting
export LINUXTEST

when i do a
echo $LINUXTEST i get 'Scripting'


in hnz /etc/profile environment variable is defined as
Code:
LINUXTEST=String
export LINUXTEST
echo $LINUXTEST i get 'String'

i'm confused as to how this is possible cos i thought i should be accessing the same profile file in /etc?

PROBLEM 2

for user 'test':
i tried setting LINUXTEST within the user scope, in the ~/.profile file to
Code:
LINUXTEST=Edition 
export LINUXTEST
logged out and logged back but its still printing 'Scripting' ?


any help is greatly appreciated!

thanks!

Last edited by greenpool; 07-31-2011 at 06:08 AM. Reason: syntax error
 
Old 07-31-2011, 03:39 AM   #10
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by greenpool View Post
I thought System wide environment variables goes into ~/.profile or ~/.bash_profile and functions and aliases go into ~/.bashrc?
The ~ in ~/.profile is shell shorthand for "the current user's home directory" so any files under it are user-specific, not system-wide.

The system-wide configuration directory is /etc so /etc/profile (and whatever it sources) is where system-wide settings are made.

From the GNU Bash Reference Manual:

"When Bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable"

and

"When an interactive shell that is not a login shell is started, Bash reads and executes commands from ~/.bashrc, if that file exists".

It is common practice to set up /etc/profile to source ~/.bashrc so logon shells are configured the same way as interactive non-logon shells. Alternatively it can be left for individual users to set up their own ~/.bash_profile to source ~/.bashrc so it is under their control.

Aliases cannot be exported so they must be set up in each shell they are required in, hence they are commonly defined in ~/.bashrc. This leads to the concept that ~/.bashrc is "for aliases". It would be truer to say that ~/.bashrc is a good choice for aliases.

Functions can be exported so they can be defined and exported in the bash login shell startup files or defined in the bash non-logon interactive shell startup files.

The bash login shell startup files are /etc/profile plus whatever it sources and the first one of ~/.bash_profile, ~/.bash_login and ~/.profile that exists plus whatever it sources. The bash non-logon interactive shell startup files are ~/.bashrc plus whatever it sources.
 
Old 07-31-2011, 04:19 AM   #11
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
You write as if each of the two users has their own /etc/profile; do you mean ~/.profile?

There's an : instead of a = in LINUXTEST:Edition
 
Old 07-31-2011, 06:07 AM   #12
greenpool
Member
 
Registered: Sep 2010
Posts: 41

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by catkin View Post
You write as if each of the two users has their own /etc/profile; do you mean ~/.profile?

There's an : instead of a = in LINUXTEST:Edition
i meant ~/.profile, sorry if that was not clear!

and yes it was meant be '=' not ':'
 
Old 07-31-2011, 09:03 AM   #13
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by greenpool View Post
i meant ~/.profile, sorry if that was not clear!

and yes it was meant be '=' not ':'
OK, so far so confusing, especially as you have edited your post since I asked about it.

Please post the relevant parts of /etc/profile, /home/test/.profile and /home/hnz/.profile and tell us how you did your tests -- using virtual terminals (via Alt+F<n> or, from the GUI, Alt+Ctrl+F<n>) or terminal emulators from the GUI desktop. For these tests it is essential to know if you logged on or started a post-logon interactive shell (the logon could be when you logged on to the GUI desktop).

Do .bash_profile or .bash_login files exist in /home/test/ or /home/hnz/ (if they do, their .profile file will not be used)?
 
  


Reply

Tags
environment variable, profile



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
environmental variable Imtiaz Deen Linux - General 2 11-11-2009 08:57 AM
environmental variable myfoot Fedora 1 01-31-2008 05:36 PM
Help: Linux Environmental Variable jkkunil Programming 2 08-01-2007 04:14 PM
linux environmental variable surender Linux - General 2 05-07-2007 07:54 AM
Setting an environmental variable maevious Linux - Newbie 6 11-24-2005 04:15 PM

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

All times are GMT -5. The time now is 09: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