Problems with bash path when using 'su'

4

1

In Ubuntu Lucid Lynx, I'm trying to do

su -l user -c "foo.sh args"

where foo.sh is an executable bash script on the PATH set in ~user/.bashrc. Unfortunately, this fails because it can't find foo.sh. If I su -l user and manually type foo.sh args at the command line, it works fine. The PATH augmentation is done in ~user/.bashrc and appears to work whether or not I su with -l interactively, but it doesn't appear to work when I specify the command.

Any idea why this is, and how I can get around that?

Thuktun

Posted 2010-09-08T03:29:08.760

Reputation: 41

Answers

2

It's starting the shell as a non-interactive login shell so it processes ~/.profile and not ~/.bashrc. You can set your PATH in that file or do it in a common file that both of them source. Some people/distributions set one startup file to source the other one, but I don't recommend this.

From man bash:

When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes com‐ mands 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.

Paused until further notice.

Posted 2010-09-08T03:29:08.760

Reputation: 86 075