8

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?

1
  • 4
    Should that be ~/.bashrc? Probably for PATH I'd put it in ~/.bash_profile. Either way, the dot is necessary.
    – mpez0
    Apr 12, 2010 at 20:58

4 Answers 4

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`
2
  • 3
    There 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. Apr 12, 2010 at 23:24
  • I think you meant ~/.bash_profile with an underline, not a hyphen. Oct 22, 2013 at 14:11
4

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.


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
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)

1
  • bash won't look for ~/.bashrc if there's a ~/.bash_profile. Oct 22, 2013 at 14:13

You must log in to answer this question.