LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Ubuntu etc/profile: environmental variable doesn't display content (https://www.linuxquestions.org/questions/linux-newbie-8/ubuntu-etc-profile-environmental-variable-doesnt-display-content-894397/)

greenpool 07-28-2011 09:58 PM

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 :confused:


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!

A.Thyssen 07-29-2011 01:25 AM

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!

greenpool 07-29-2011 02:11 AM

added env variable to ~/.profile still doesn't work
 
Quote:

Originally Posted by A.Thyssen (Post 4428232)
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!

akuma_linux 07-29-2011 02:54 AM

Hi greenpool,

Try adding the environment variables into /etc/environment

catkin 07-29-2011 03:20 AM

Quote:

Originally Posted by A.Thyssen (Post 4428232)
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).

greenpool 07-30-2011 06:17 PM

Thanks for the info guys.

I shall play around and see if it works!

rkski 07-30-2011 06:24 PM

Put it in ~/.bashrc and make sure to start a new shell before trying to 'echo'.

greenpool 07-30-2011 07:06 PM

Quote:

Originally Posted by rkski (Post 4429573)
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?

greenpool 07-30-2011 07:12 PM

So i managed to get my original question working but now i'm facing another dilemma :banghead:

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!

catkin 07-31-2011 03:39 AM

Quote:

Originally Posted by greenpool (Post 4429583)
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.

catkin 07-31-2011 04:19 AM

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

greenpool 07-31-2011 06:07 AM

Quote:

Originally Posted by catkin (Post 4429786)
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 ':'

catkin 07-31-2011 09:03 AM

Quote:

Originally Posted by greenpool (Post 4429845)
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)?


All times are GMT -5. The time now is 08:30 AM.