16

Bash will source automatic profiles such as .bashrc. --rcfile option can be used to override the automatic script. But I need to source additional personalized file (that's the automatic script plus another file) when launching the bash shell without touching ANY files in $HOME or /etc directory since $HOME directory belongs to application run user. The personalized file must not be located in $HOME directory. Is this possible?

I tried:

    /bin/bash <<EOF
    . /a-directory-outside-of-home/vanilla
    EOF

but it returned to the current shell.

2
  • 3
    Can you source the extra file from within your .bashrc? Commented Nov 27, 2015 at 16:14
  • that's not a option. I want to source another script without touching the .bashrc. Commented Nov 27, 2015 at 16:32

4 Answers 4

11

Okay, so you want to run the user's normal .bashrc, followed by your own script, and you want to trigger this behavior in the way that bash is called, correct?

The call:

/bin/bash --rcfile myscript

First line of myscript:

source $HOME/.bashrc
Sign up to request clarification or add additional context in comments.

Comments

9
bash --rcfile <(cat rcfile1; cat rcfile2)

works just fine and requires no modifications anywhere.

2 Comments

Nice to enter a python virtualenv in one step. Btw. you don’t need the ; as cat concatenates all inputs by default. bash --rcfile <(cat ~/.bashrc env/bin/activate)
Or as a handy function to add to your .bash_aliases: enterVE(){ [[ -n $1 && -a "$1/bin/activate" ]] && bash --rcfile <(cat ~/.bashrc $1/bin/activate) || echo 'No virtual environment path given as parameter'; }
0

Add your stuff as *.sh files in /etc/profile.d -- read /etc/profile and http://www.gnu.org/software/bash/manual/bashref.html#Bash-Startup-Files

1 Comment

Also, one cannot assume that the user has root access to modify /etc
-1

I wouldn't modify the .bashrc file. Instead, I would modify the .bash_profile file which is located at $HOME.

It is the place from where .bashrc is included.

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
  . ~/bashrc
fi

A good option for you is to add a .personalized_settings file and include it just below the above lines like this.

# Adding personalized settings
if [ -f ~/.personalized_settings ]; then
  . ~/personalized_settings
fi

This requires that .personalized_settings file is located at $HOME.

I have tested the above in Fedora12.

Edit : You might need to look for .profile instead of .bash_profile in Ubuntu (and hopefully other Debian based systems). (courtesy @Benjamin W.)

This (link)[https://www.gnu.org/software/bash/manual/bash.html#Bash-Startup-Files] will give you more information on it.

What if you mess around with any of the profile files?

You have a back-up copy of all these files in /etc/skel which you could use to restore.

3 Comments

It's called .profile in Ubuntu, see for example askubuntu.com/questions/510709/…
Thanks for that info.. It's difficult in that these names are not standard.. Redhat and it clones have one way of doing it, Debian based system has another..
The order in which Bash tries to find them is specified, though: gnu.org/software/bash/manual/bash.html#Bash-Startup-Files

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.