41

I have a user, say user1, which has modifications to its .bash_profile, one of them changing the PATH, e.g.: export PATH=/some/place:$PATH. This change works fine if I log on as user1 or do a su - user1.

But if I try to run a command via su as root, e.g.:

su -c test.sh oracle

(test contains echo $PATH)

It doesn't seem to have the modified PATH (or root's PATH, for that matter). I've also tried copying .bash_profile to .profile, to no avail.

Why is this happening?

60

Using su without -l or - starts bash as an interactive, but non-login shell, which doesn't read from either of the files you specified. Use the -l or - option or put the relevant config into /root/.bashrc.

Quick summary of config files:

  • Login shell (-l/--login) reads /etc/profile first, and then the first it finds of: ~/.bash_profile, ~/.bash_login, and ~/.profile.
  • Interactive but non-login shell (-i) reads /etc/bash.bashrc and ~/.bashrc, in that order (unless the --rcfile option is used and tells it to look elsewhere).
  • Non-interactive shells, e.g. started from within another program without using the -l or -i flags, reads the file specified in the BASH_ENV environment variable.
  • When run as sh as a login shell, it will read /etc/profile and ~/.profile, in that order.
  • When run as sh as an interactive non-login, it reads the file specified in ENV.
5

Bash behaves differently depending on if it believes that it is a login shell, i.e. the first shell run when you log onto a system. It only reads .bash_profile if it is a login shell. If you put the PATH-changing code into .bashrc instead, it will be run for all interactive bash shells, not just login shells.

0

If using the Gnome environment in Scientific Linux 6 (or presumably RHEL 6), start a terminal. Go to Edit -> Profile Preferences -> "Title and Command" tab. Make sure that the checkbox "Run command as a login shell" is checked. I found that the Gnome terminal application is ignoring my .bash_profile unless I do this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.