Bash Manual says:
Bash attempts to determine when it is being run with its standard input connected to a network connection, as when executed by the remote shell daemon, usually
rshd
, or the secure shell daemonsshd
. If Bash determines it is being run in this fashion, it reads and executes commands from~/.bashrc
, if that file exists and is readable.
This Bash sources ~/.bashrc
:
ssh user@host :
But this Bash sources ~/.bash_profile
:
ssh user@host
I don't see a difference in these two commands according to the spec. Isn't stdin connected to a network connection in both cases?
Best Answer
A login shell first reads
/etc/profile
and then~/.bash_profile
.A non-login shell reads from
/etc/bash.bashrc
and then~/.bashrc
.Why is that important?
Because of this line in
man ssh
:In other words, if the ssh command only has options (not a command), like:
It will start a login shell, a login shell reads
~/.bash_profile
.An ssh command which does have a command, like:
Where the command is
:
(or do nothing).It will not start a login shell, therefore
~/.bashrc
is what will be read.Remote stdin
The supplied tty connection for /dev/stdin in the remote computer may be an actual tty or something else.
For:
Which ends in a TTY (not a network connection) as the started bash sees it.
For a ssh connection with a command:
The list of TTY's start the same, but note that /etc/profile was not sourced.
Which tells the shell that the connection is a pipe (not a network connection).
So, in both the test cases, the shell is unable to know that the connection is from a network and therefore does not read
~/.bashrc
(if we only talk about the connection to a network). It does read ~/.bashrc, but for a different reason.