Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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 the root's PATH, for that matter). I've also tried copying .bash_profile to .profile, to no avail.

Why is this happening?

share|improve this question
    
use --login switch to su –  Nikhil Mulley Jan 23 '12 at 17:01
    
FYI -- nice writeup on bash configuration files hacktux.com/bash/bashrc/bash_profile –  Nikhil Mulley Jan 23 '12 at 17:02

3 Answers 3

up vote 13 down vote accepted

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.
share|improve this answer

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.

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

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

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