Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.bashrc at ssh login

Tags:

bash

ssh

ubuntu

When I ssh into my ubuntu-box running Hardy 8.04, the environment variables in my .bashrc are not set.

If I do a source .bashrc, the variables are properly set, and all is well.

How come .bashrc isn't run at login?

like image 224
Hobhouse Avatar asked May 04 '09 15:05

Hobhouse


People also ask

Does bashrc run on SSH?

bashrc is sourced when you SSH in, by default.

What is the ~/ bashrc file?

A bashrc file is shell script that Bash runs whenever it is started. Along with setting in the OS, the bashrc helps determine how your command line interface (CLI) or Terminal app looks and acts.

Should I use bashrc or bash_profile?

bash_profile is read and executed when Bash is invoked as an interactive login shell, while . bashrc is executed for an interactive non-login shell. Use . bash_profile to run commands that should run only once, such as customizing the $PATH environment variable .


2 Answers

.bashrc is not sourced when you log in using SSH. You need to source it in your .bash_profile like this:

if [ -f ~/.bashrc ]; then   . ~/.bashrc fi 
like image 176
Ayman Hourieh Avatar answered Sep 28 '22 01:09

Ayman Hourieh


I had similar situation like Hobhouse. I wanted to use the command

ssh myhost.com 'some_command' 

where some_command exists in /var/some_location.

I tried to append /var/some_location to the PATH environment variable by editing $HOME/.bashrc but that wasn't working. Because per default, .bashrc (on Ubuntu 10.4 LTS) exits early due to this piece of code:

# If not running interactively, don't do anything [ -z "$PS1" ] && return 

Meaning if you want to change the environment for the ssh non-login shell, you should add code above that line.

like image 26
3 revs, 2 users 80% Avatar answered Sep 28 '22 02:09

3 revs, 2 users 80%