Why is /etc/profile not being loaded during non-login bash shell sessions?
For 11.04, I did a fresh install of my system. Part of that install was to install rvm, which sticks a rvm.sh in /etc/profile.d/ . This doesn’t work as /etc/profile (which loads each +r in /etc/profile.d/*.sh ) is not being loaded. According to the documentation, the profile is only sourced if bash is run in login. To verify this, I invoked bash —login , after which rvm was available. This has worked for me in previous versions of Ubuntu without any configuration. That is, a fresh install of 10.10 will correctly source profile/.d. My question is: is there anything I’m doing wrong, or are there some new assumptions being made in Natty that have broken this? My current workaround is to source /etc/profile in ~/.bashrc (which is awful as profile is meant to load before bashrc’s, but does the trick).
6 Answers 6
Per default, gnome-terminal does not start bash as a login shell (I assume you mean bash started inside a gnome-terminal). That means bash will not read /etc/profile or ~/.profile . As you have correctly observed, bash will read those files if started as a login shell.
The quick fix to your problem is to configure gnome-terminal to start bash as a login shell so it will read /etc/profile . To do so click on the Edit menu of gnome terminal, then «Profile Preferences», then enable «Run Command as a login shell».
I do not recommend doing this because it messes up the distinction between ~/.profile and ~/.bashrc .
Ideally ~/.bashrc should do setup for bash, typically required everytime bash is started. While ~/.profile should do stuff which is not bash and required only once, during login.
From your problem description it seems that the rvm script needs to be loaded only once, during login. As far as I know Ubuntu has configured the graphical login to read /etc/profile/ and ~/.profile . That means, after logging out and a logging in once, the rvm script should be active.
If it still doesn’t work, then perhaps the rvm script needs to be loaded for every bash session. If that is the case then bashrc is the more appropriate place for the script.
Setting ‘run command as a login shell’ did indeed load /etc/profile (and thus the .d’s). Thanks for the explanation — it seems like this requires follow up with the rvm folk.
Ubuntu 11.04 doesn’t seem to read /etc/profile and ~/.profile during login. It’s strange because it always worked with older versions. I think this is not a solution but a workaround.
@lesmana Is there a way I could enable «Run Command as a login shell» from CLI instead of GUI? I’m sshed into a remote server and cant forward X locally.
There is, however, a file /etc/bash.bashrc that is read by gnome-terminal and is the «system-wide .bashrc file for interactive bash(1) shells.»
My call to the rvm function, [[ -s «$HOME/.rvm/scripts/rvm» ]] && . «$HOME/.rvm/scripts/rvm» , went in there, and works just fine for the couple of users on that system.
gnome-terminal does nothing of the sort. gnome-terminal runs bash (or whatever your login shell is set to in the passwd database) in interactive mode, and bash started interactively reads /etc/bash.bashrc . Well, at least on Debian/Ubuntu. Bash normally does not read a system wide bashrc; debian has patched it to do so.
@geirha : dmitri@Eos:~$ uname -a Linux Eos 3.0.0-16-generic #29-Ubuntu SMP Tue Feb 14 12:48:51 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux dmitri@Eos:~$ cat /etc/bash.bashrc # System-wide .bashrc file for interactive bash(1) shells. # To enable the settings / commands in this file for login shells as well, # this file has to be sourced in /etc/profile. # If not running interactively, don’t do anything [ -z «$PS1» ] && return .
Yes, obviously, gnome-terminal invokes bash to read that file: if your shell is ksh, then gnome-terminal wouldn’t read that file, would it? May I suggest looking up the word «pedant» in the dictionary before you continue using provocative phrases like «does nothing of the sort.»
Installing RVM as multiuser requires the user to run this command:
(because Ubuntu does not source /etc/profile.d at the login)
echo '[[ -s "/etc/profile.d/rvm.sh" ]] && . "/etc/profile.d/rvm.sh" # Load RVM function' >> ~/.bashrc
Ubuntu does source the files in /etc/profile.d/ during login because the default /etc/profile loops through, and sources, the files in /etc/profile.d/ . However, if this rvm.sh contains code for interactive shells, such as functions or aliases, having them sourced by /etc/profile is pointless. Setup for interactive bash sessions must be in .bashrc .
if you have ZSH or OH-MY-ZSH, your profile is /etc/zprofile
I have actually found for those who do not like to fully restart their boxes unless it is absolutely required the following.
You do not need to restart the whole machine to have changes to /etc/profile take effect. All you need is to have your XWindows Desktop GUI restart so it re-reads configs (as it would if it were to be started as part of a system-wide restart).
For me I use xUbuntu which uses LightDM but you can sub in whatever executable calls your current desktop.
First, you can not use a terminal emulator from inside the desktop GUI itself to do this (at least not with LightDM) you need to either use SSH or a secondary console (if you hold Ctrl + Alt and hit an F Key or number key you should drop away from your graphical desktop login or screen to a plain terminal window). I also suggest you close out any remote desktop stuff like VNC if you are using SSH (obviously SSH can be connected you just do not want an open VNC window to be polling display 0 while it is trying to restart as this may hang the restart while it waits for you to terminate the VNC session).
I do this all the time with SSH and it is a big lifesaver especially when you are not physically by your machine and do not feel like waiting for a full restart and reconnecting 😀
Now the easy part once in a «secondary» terminal or SSH issue: (Yes, this will kill your current session, be sure you do not mind, save any open documents or work)
sudo service lightdm restart
wait a minute as it kills the desktop and then starts it again (this can take a few good seconds as things are stopped and then executed again on start)
That’s it, now when you head back to either console 0 or you go to to use VNC to connect again you will be greeted by the login prompt and login as normal and if you open a terminal emulator and go to issue your command you should now have no problems as everything was re-sourced just as if you did a full machine restart.
Just remember, if you issue sudo service lightdm restart [or stop , start , whatever] from within the desktop environment itself using a terminal emulator the command will just hang there and nothing will seem to be happening. This is because you are currently using that console (aka display) 0 and so it can not fully go down and come back up as it is waiting for the desktop to not be in use before it runs the command.
Last Note: I have not tried to issue restart on the environment from within the desktop in a terminal emulator and then switch to another console that may then restart it, but if you are already going to switch the console then why not just run the command from there as it should be anyways?
Load .bash_profile for every terminal
I set up some aliases in my .bash_profile on my Max OS X. It works but when I’m opening a new tab I always have to load my .bash_profile file with this command:
How can I make it work for every terminal I’m opening, even if I’m restarting my Mac or Linux computer?
Add your stuff to ~/.profile . Or add : [[ -r ~/.bash_profile ]] && . ~/.bash_profile in ~/.profile .
@l3x, what is the correct sequence of sourcing these init scripts? One may end up sourcing same files recursively.
4 Answers 4
If you use zsh , you can add source ~/.bash_profile at the end of .zshrc file at the following path: /Users/YOUR USER NAME/.zshrc , then restart your Terminal/iTerm2 app.
Note that this file is hidden. You can press CMD + SHIFT + . in Finder to see it, Or just open it in default text editor with the following command from terminal:
open ~/.zshrc
You don’t need to do this by hand, run the following command:
echo "source ~/.bash_profile" >> ~/.zshrc
* Dont forget to restart your terminal.
The files executed at the start may be several, usually ~/.bashrc for interactive, non-login shells. The kind I assume you are using.
If so, create an ~/.bashrc file and source ~/.bash_profile in it:
if [ -f ~/.bash_profile ]; then . ~/.bash_profile fi
Study this image, as it is a condensed guide
If you do need to find out exactly which file is being executed, take a look at this web page. It has a lot of specific tests to find out what file is setting what.
Specific for Mac-OS (which is an exception and loads ~/.bash_profile) do as is recomended in the site linked in this answer AFTER you confirm that your bash is doing as explained there.
Import .bashrc from .bash_profile , not the other way around. A login shell is a special case of an interactive shell. If you are using .profile instead of .bash_profile , make a .bash_profile that simply imports .profile .
@chepner The user is asking for a way to load ~./bash_profile instead of doing it manually. So, it could not be the other way around. The file ~./bash_profile is not being loaded (acording to the user).
I know this is a pretty old post, but this problem comes and goes quite oftenly and a lot of laborous solutions are offered. The fact is: being aware of a simple info would solve this pretty fast and easy:
LINUX/Unix OS will load the profile files on startup following the rules below (some distros may have other files names, mainly for user local profiles, but the main rule follows this):
First and foremost: /etc/profile is loaded (global settings); Next: ˜/.bash_profile (local user settings- other files may be found, like ˜/.profile , depending on the distro. Check documentation).
So, if you are in a Login Shell environment, put all your crazy stuff inside ˜/.bash_profile (or the file provided by your distro) and everything will be fine.
First and foremost: /etc/bashrc (some distros will load bash.bashrc ); The next file to be seeked and loaded is ˜/.bashrc
And that’s why so many people (just like me) got frustrated having to source their ˜/.bash_profile every single time a Terminal was loaded. We were simply inserting info in the «wrong» file- regarding the kind of shell loaded (see above: login, non-login).
More details on variants of the files mentioned above and some info on login vs non-login shell can be found here.