4

I have a CentOS VM to which I log in using SSH. I appended the following line to .bashrc

echo "from ~/.bashrc: (pid $$)"

and the following line to .bash_profile

echo "from ~/.bash_profile"

When I log into the VM and run ps I get the following output

user@laptop:~ $ ssh vmcentos
Last login: Sun May 17 04:48:24 2020 from 192.168.122.1
from ~/.bashrc: (pid 1821)
from ~/.bash_profile
[admin@localhost ~]$ ps -H -o pid,command
  PID COMMAND
 1821 -bash
 1844   ps -H -o pid,command
[admin@localhost ~]$

This output is what I expect since the shell I log into is an interactive login shell and thus the .bash_profile file is sourced which in turn sources the .bashrc file.

Now I log out from the VM and execute the following command

user@laptop:~ $ ssh vmcentos 'sleep 60; echo $-'
from ~/.bashrc: (pid 1901)
hBc
user@laptop:~ $

then I log into another ssh session on the VM and inspect the process table

[admin@localhost ~]$ ps -eH -o pid,command
  PID COMMAND
(... more output here...)
 1900       sshd: admin@notty
 1901         bash -c sleep 60; echo $-
 1914           sleep 60
(... more output here...)

As far as I understand the shell that ssh executes (process 1901) is non-interactive (because of the -c option and also because the $- variable contains no i character) and non-login (because ARGV0 is not -bash and no --login option is provided). Thus neither .bashrc nor .bash_profile should be sourced. Yet the output of the command clearly shows that .bashrc was sourced. Why?

I used a standard CentOS installation with the standard openssh configuration.

2
  • Related: unix.stackexchange.com/questions/332531/…
    – Kusalananda
    May 17, 2020 at 10:20
  • Side note: if you test stuff like that, remember that unconditionally printing stuff from the shell init files can break programs like scp and anything else that might try to do something automatically and interpret the output.
    – ilkkachu
    May 17, 2020 at 13:07

1 Answer 1

4

According to the manual, it's supposed to do that:

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 daemon sshd. If Bash determines it is being run in this fashion, it reads and executes commands from ~/.bashrc, if that file exists and is readable.

Bash's startup files are weird.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.