James May
James May

Reputation: 1537

Strange behavior of .bashrc

I changed .bashrc file on my web server a little bit, to color links on ls -la and so on. But when I log in using ssh: ssh user@server and type ls -al nothing is coloring, seems like my .bashrc file has not been applied on login. When if I just type bash and then again ls -la - all works fine. In short, all my rules in .bashrc only apllied when I type bash just after authorization, a little boring.

Upvotes: 1

Views: 118

Answers (2)

Thomas
Thomas

Reputation: 181785

~/.bashrc is only read if the shell is interactive and not a login shell:

When an interactive shell that is not a login shell is started, bash reads and executes commands from /etc/bash.bashrc and ~/.bashrc, if these files exist.

Furthermore:

Bash attempts to determine when it is being run with [...] sshd. If bash determines it is being run in this fashion, it reads and executes commands from ~/.bashrc and ~/.bashrc, if these files exist and are readable. It will not do this if invoked as sh.

So:

  • your remote shell must be bash, not sh,
  • it must not be a login shell, and
  • it must be an interactive shell.

Upvotes: 1

tkocmathla
tkocmathla

Reputation: 911

When you log in via ssh, you invoke a login shell. When you type bash in an existing shell, you invoke an interactive shell.

.bash_profile is read when a login shell is invoked, and .bashrc is read when an interactive shell is invoked.

Try adding this to your .bash_profile:

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

See bash(1) for more details.

Upvotes: 3

Related Questions