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?
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.
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"?
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.
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.
.bash_rc
or .bashrc
? On Fedora/Red Hat it's the last.
Sep 23, 2009 at 15:24
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
~/.bash_profile
?