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?
There is the
~/.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
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`
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.
There are already several (probably correct) answers but I had this exact same problem and this is what worked for me:
~/.bash_profile the following lines:
if [ -f ~/.bashrc ]; then source ~/.bashrc fi
(note, in my $HOME directory I already had the following files: