1

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?

1
  • did you mean ~/.bash_profile ? Sep 23, 2009 at 15:22

5 Answers 5

2

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.

1
  • The advice is not universal. For example, Slackware Linux doesn't have PAM.
    – Anonymous
    Oct 10, 2009 at 9:59
2

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"?

1

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.

1

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.

2
0

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

You must log in to answer this question.