Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

How can ensure that a environment variable is set for a user? I have

MYVAR=whatever
export MYVAR

in the ~/.bash_profile but a report from the user suggests that it hasn't worked for them. What could have happened?

share|improve this question
    
did you mean ~/.bash_profile ? –  Pascal Thivent Sep 23 '09 at 15:22
    
Yes, I've edited it now. Thanks. –  jjujuma Dec 3 '09 at 14:36

5 Answers 5

up vote 2 down vote accepted

Use the PAM module called pam_env to set enviroment variables on login. If you use this solution, the environment variables will be available to GUI programs launched directly from the "Window Manager" (e.g. GNOME, KDE) too, not only to programs launched from a terminal. Also, the user can use another shell, not only Bash. Maybe this article from O'Reilly can help you.

share|improve this answer
    
The advice is not universal. For example, Slackware Linux doesn't have PAM. –  Anonymous Oct 10 '09 at 9:59

Some excerpts from the bash manpage:

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. The --noprofile option may be used when the shell is started to inhibit this behavior.

...

When an interactive shell that is not a login shell is started, bash reads and executes commands from /etc/bash.bashrc and ~/.bashrc, if these files exist. This may be inhibited by using the --norc option. The --rcfile file option will force bash to read and execute commands from file instead of /etc/bash.bashrc and ~/.bashrc.

Could this explain why it "hasn't work for them"?

share|improve this answer

From within your code, you could do:

if [ x"$MYVAR" = "x" ]
    then
    echo "it didn't work"
    exit 1
fi
echo "it did work: $MYVAR"

if you're setting it from something like the user's own .bash_login or .bashrc, are they sudo'ing out of their environment?

Exporting a variable will only work if the subsequent use of that variable is dependent to the export; i.e., export during their login process should work for anything they run, unless they su/sudo, in which case they're actually leaving that environment and then executing whatever they're doing.

share|improve this answer

It should probably be set in ~/.bashrc if it's not the login shell, for example if they are running a terminal in Gnome.

Also, you list the file as ./bash_profile but that gives no indication of where that file is (it says it's in the current directory). I assume you mean /etc/bash_profile. That file is only used for login shells or non-interactive ones.

share|improve this answer
    
Is it .bash_rc or .bashrc? On Fedora/Red Hat it's the last. –  Cristian Ciupitu Sep 23 '09 at 15:24
    
oops, typo –  Dennis Williamson Sep 23 '09 at 17:04

jjujuma

First ensure that the user is using bash. He/She might be using csh. If you want it set for just that user and they are using bash/sh then you need to put it in the .bashrc file. If csh/tcsh you need to put it in the .cshrc/.tcshrc. If you want it set globally for all users /etc/profile and /etc/csh.cshrc for bash/tcsh respectfully will get you up and going!

HTH

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.