How do I permanently set my bashrc changes?

8

3

Everytime I log into SSH, I have to manually source the bashrc file to set my path variables the way I want to. How do I have bash do it automatically? Shouldn't it be automatic?

user34016

Posted 2010-04-12T19:56:56.230

Reputation:

4Should that be ~/.bashrc? Probably for PATH I'd put it in ~/.bash_profile. Either way, the dot is necessary. – mpez0 – 2010-04-12T20:58:32.167

Answers

5

There is the ~/.profile (or ~/.bash-profile) file, that runs at every login. You should set environment variables there (with the export command). And there is the ~/.bashrc file, which is run on opening each sell. Commands that are not inherited to all subshells, like alias, can be set here (though for good practise, aliases should be set in ~/.aliases, which is automatically sourced by ~/.bashrc).

If it doesn't work for you, you either use a wrong filename (missing "." from beginning?), or you don't use bash as your shell. In the latter case, try passwd -s /bin/bash (or chsh -s /bin/bash, depending on OS), or call the SSH the following way: 

ssh username@host bash`

petersohn

Posted 2010-04-12T19:56:56.230

Reputation: 2 554

3There should be a dot: ~/.bashrc (you even mention that the OP might be missing a dot!). Also, ~/.aliases or ~/.bash_aliases is not automatically sourced. There has to be instructions in ~/.bashrc to explicitly do that. – Paused until further notice. – 2010-04-12T23:24:54.013

I think you meant ~/.bash_profile with an underline, not a hyphen. – Cristian Ciupitu – 2013-10-22T14:11:15.853

3

You should also know about $BASH_ENV .

What files are read by bash when it starts up depends on if the session is interactive or not. Occasionally there can be an issue where it's not clear if the session is interactive. So besides ~/.bashrc you might want to also export and set variable BASH_ENV to point to a file containing the PATH and other settings that you need. BASH_ENV can be set to point to your .bashrc file (sometimes it's .bash_rc) so long as there are no interactive commands in there (to be safe use a separate file, say ~/.bash_env). From the bash man pages:


When an interactive shell that is not a login shell is started, bash reads and executes commands from ~/.bashrc, if that file exists. This may be inhibited by using the --norc option. The --rcfile file option will force bash to read and execute commands from file instead of ~/.bashrc.

When bash is started non-interactively, to run a shell script, for example, it looks for the variable BASH_ENV in the environment, expands its value if it appears there, and uses the expanded value as the name of a file to read and execute. Bash behaves as if the following command were executed:

  if [ -n "$BASH_ENV" ]; then . "$BASH_ENV"; fi

but the value of the PATH variable is not used to search for the file name.


Marnix A. van Ammers

Posted 2010-04-12T19:56:56.230

Reputation: 1 978

1

I know this is old, but i think on a new login shell, bash looks for a single file to run. It looks for .bash_profile, then .profile, then .bashrc. If it finds .bash_profile or .profile, it won't further look for .bashrc.

Solution, put in your .profile:

[ -f $HOME/.bashrc ] && . $HOME/.bashrc

Rich Homolka

Posted 2010-04-12T19:56:56.230

Reputation: 27 121

1

There are already several (probably correct) answers but I had this exact same problem and this is what worked for me:

add to ~/.bash_profile the following lines:

if [ -f ~/.bashrc ]; then
   source ~/.bashrc
fi

See here and here for more info.

(note, in my $HOME directory I already had the following files: .bash_aliases .bash_history .bash_logout .bash_profile .bashrc)

hoosierEE

Posted 2010-04-12T19:56:56.230

Reputation: 344

bash won't look for ~/.bashrc if there's a ~/.bash_profile. – Blacklight Shining – 2013-10-22T14:13:21.260